Minimizing Sum with Neighbor Value Priority in Array Transformation
Last Updated :
28 Dec, 2023
Given an array arr[] of length n, where arr[i] represents some value. You have to create an array result[] of length n, such that it follows both conditions given below and the task is to find the minimum sum of the result[] while ensuring that these rules are satisfied.
- result[i] >= 1, for all i, such that 0 <= i <= n
- result[i] with a higher arr[i] get more value than their neighbors.
Examples:
Input: arr = {1,0,2}
Output: 5
Explanation: We can create result[] = {2, 1, 2}, it follows both given conditions.
Input: arr = {1,2,2}
Output: 4
Explanation: We can create result[] = {1, 2, 1}, it follows both given conditions.
Input: arr = {1,2,3}
Output: 4
Explanation: We can create result[] = {1, 2, 3}, it follows both given conditions.
Approach: To solve the problem follow the below idea:
Our aim to ensure that elements with higher values receive more items than their neighbors, while satisfying the constraint that each element receives at least one item. To achieve this, we have to calculates left and right counts for each element and then computes the minimum sum of items needed to meet these conditions.
Step-by-step approach:
- Create a variable totalSum for answer.
- Create two array, leftCounts[] and rightCounts[], both of size n, and initialize them to 1.
- Iterate from starting indexing of arr[].
- If the current element is greater than the previous one, update leftCounts for the current element as leftCounts[i] = leftCounts[i – 1] + 1.
- Iterate arr[] in reverse order.
- If the current element is greater than the next one, update rightCounts for the current element as rightCounts[i] = rightCounts[i + 1] + 1.
- Iterate through each element of arr.
- Add the maximum value between leftCounts[i] and rightCounts[i] to totalSum.
- Return totalSum as the minimum sum needed.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumSum(vector< int >& arr)
{
int n = arr.size();
vector< int > resultLeft(n, 1), resultRight(n, 1);
int totalSum = 0;
for ( int i = 0; i < n; i++) {
if (i - 1 >= 0 && arr[i] > arr[i - 1]) {
resultLeft[i] = resultLeft[i - 1] + 1;
}
if (n - i < n && arr[n - i - 1] > arr[n - i]) {
resultRight[n - i - 1] = resultRight[n - i] + 1;
}
}
for ( int i = 0; i < n; i++) {
totalSum += max(resultLeft[i], resultRight[i]);
}
return totalSum;
}
int main()
{
vector< int > arr = { 1, 2, 3 };
int minSum = minimumSum(arr);
cout << minSum << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
static int minimumSum( int [] arr) {
int n = arr.length;
int [] resultLeft = new int [n];
int [] resultRight = new int [n];
Arrays.fill(resultLeft, 1 );
Arrays.fill(resultRight, 1 );
int totalSum = 0 ;
for ( int i = 0 ; i < n; i++) {
if (i - 1 >= 0 && arr[i] > arr[i - 1 ]) {
resultLeft[i] = resultLeft[i - 1 ] + 1 ;
}
if (n - i < n && arr[n - i - 1 ] > arr[n - i]) {
resultRight[n - i - 1 ] = resultRight[n - i] + 1 ;
}
}
for ( int i = 0 ; i < n; i++) {
totalSum += Math.max(resultLeft[i], resultRight[i]);
}
return totalSum;
}
public static void main(String[] args) {
int [] arr = { 1 , 2 , 3 };
int minSum = minimumSum(arr);
System.out.println(minSum);
}
}
|
Python3
def minimum_sum(arr):
n = len (arr)
result_left = [ 1 ] * n
result_right = [ 1 ] * n
total_sum = 0
for i in range (n):
if i - 1 > = 0 and arr[i] > arr[i - 1 ]:
result_left[i] = result_left[i - 1 ] + 1
if n - i < n and arr[n - i - 1 ] > arr[n - i]:
result_right[n - i - 1 ] = result_right[n - i] + 1
for i in range (n):
total_sum + = max (result_left[i], result_right[i])
return total_sum
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 ]
min_sum = minimum_sum(arr)
print (min_sum)
|
C#
using System;
class GFG
{
static int MinimumSum( int [] arr)
{
int n = arr.Length;
int [] resultLeft = new int [n];
int [] resultRight = new int [n];
Array.Fill(resultLeft, 1);
Array.Fill(resultRight, 1);
int totalSum = 0;
for ( int i = 0; i < n; i++)
{
if (i - 1 >= 0 && arr[i] > arr[i - 1])
{
resultLeft[i] = resultLeft[i - 1] + 1;
}
if (n - i < n && arr[n - i - 1] > arr[n - i])
{
resultRight[n - i - 1] = resultRight[n - i] + 1;
}
}
for ( int i = 0; i < n; i++)
{
totalSum += Math.Max(resultLeft[i], resultRight[i]);
}
return totalSum;
}
public static void Main( string [] args)
{
int [] arr = { 1, 2, 3 };
int minSum = MinimumSum(arr);
Console.WriteLine(minSum);
}
}
|
Javascript
function minimumSum(arr) {
const n = arr.length;
const resultLeft = Array(n).fill(1);
const resultRight = Array(n).fill(1);
let totalSum = 0;
for (let i = 0; i < n; i++) {
if (i - 1 >= 0 && arr[i] > arr[i - 1]) {
resultLeft[i] = resultLeft[i - 1] + 1;
}
if (n - i < n && arr[n - i - 1] > arr[n - i]) {
resultRight[n - i - 1] = resultRight[n - i] + 1;
}
}
for (let i = 0; i < n; i++) {
totalSum += Math.max(resultLeft[i], resultRight[i]);
}
return totalSum;
}
const arr = [1, 2, 3];
const minSum = minimumSum(arr);
console.log(minSum);
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...