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.
- Maximum possible GCD after replacing at most one element in the given array
- Replacing an element makes array elements consecutive
- Minimize swaps required to maximize the count of elements replacing a greater element in an Array
- Maximum sum in an array such that every element has exactly one adjacent element to it
- Maximum set bit sum in array without considering adjacent elements
- Array formed using sum of absolute differences of that element with all other elements
- Maximum number formed from array with K number of adjacent swaps allowed
- Search an element in an array where difference between adjacent elements is 1
- Maximum sum in circular array such that no two elements are adjacent
- Maximum sum in circular array such that no two elements are adjacent | Set 2
- Shuffle the position of each Array element by swapping adjacent elements
- Minimize the maximum difference between adjacent elements in an array
- Minimize the maximum absolute difference of adjacent elements in a circular array
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal
- Find Array formed by adding each element of given array with largest element in new array to its left
- Most frequent element in Array after replacing given index by K for Q queries
- Game of replacing array elements
- Maximum possible middle element of the array after deleting exactly k elements
- Array value by repeatedly replacing max 2 elements with their absolute difference
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.