Given an array arr of size N, the task is to find the maximum sum of the Array formed by replacing each element of the original array with the sum of adjacent elements.
Input: arr = [4, 2, 1, 3]
Replacing each element of the original array with the sum of adjacent elements:
4 + 2 = 6
6 + 1 = 7
7 + 3 = 10
Array formed by replacing each element of the original array with the sum of adjacent elements: [6, 7, 10]
Therefore, Sum = 6 + 7 + 10 = 23
Input: arr = [2, 3, 9, 8, 4]
Replacing each element of the original array with the sum of adjacent elements to get maximum sum:
9 + 8 = 17
17 + 4 = 21
21 + 3 = 24
24 + 2 = 26
Array formed by replacing each element of the original array with the sum of adjacent elements: [17, 21, 24, 26]
Therefore, Sum = 17 + 21 + 24 + 26 = 88.
- Scan through the array to pick the adjacent pair with the highest sum.
- From there on, using Greedy algorithm, pick the left or right integer, whichever is greater.
- Repeat the process till only a single element is left in the array.
Below is the implementation of the above approach:
Time complexity: O(N)
space complexity: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Reduce array to a single element by repeatedly replacing adjacent unequal pairs with their maximum
- Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum
- Count ways to split array into two equal sum subarrays by replacing each array element to 0 once
- Find Array formed by adding each element of given array with largest element in new array to its left
- Form an array of distinct elements with each element as sum of an element from each array
- Maximize product of array by replacing array elements with its sum or product with element from another array
- Array formed from difference of each element from the largest element in the given array
- Queries to print count of distinct array elements after replacing element at index P by a given element
- Maximize array sum by replacing equal adjacent pairs by their sum and X respectively
- Maximum Subarray Sum possible by replacing an Array element by its Square
- Maximize modulus by replacing adjacent pairs with their modulus for any permutation of given Array
- Smallest array that can be obtained by replacing adjacent pairs with their products
- Shuffle the position of each Array element by swapping adjacent elements
- Maximum possible GCD after replacing at most one element in the given array
- Minimize swaps required to maximize the count of elements replacing a greater element in an Array
- Replacing an element makes array elements consecutive
- Maximum sum in an array such that every element has exactly one adjacent element to it
- Maximum number formed from array with K number of adjacent swaps allowed
- Sum of product of each element with each element after it
- Minimize remaining array element by repeatedly replacing pairs by half of one more than their sum
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.