Minimum sum possible by assigning every increasing/decreasing consecutive pair with values in that order
Given an array arr[] of size N, the task is to find the minimum sum of positive integers that can be assigned to each array element arr[i], such that if arr[i] > arr[i+1] or arr[i – 1], then the positive integer assigned to arr[i] must exceed the integer assigned to arr[i + 1] or arr[i – 1].
Examples:
Input: arr[] = {1, 0, 2}
Output: 5
Explanation: One possible way to assign positive integers is ans[] = {2, 1, 2} such that the following conditions are satisfied:
- arr[0] > arr[1] and ans[0] > ans[1]
- arr[1] < arr[2] and ans[1] < ans[2]
Therefore, minimum possible sum = 2 + 1 + 2 = 5.
Input: arr[] = {1, 2, 2}
Output: 4
Explanation: One possible way to assign positive integers is ans[] = {1, 2, 1}. Therefore, the minimum possible sum = 1 + 2 + 1 = 4.
Approach: The idea is to traverse the given array from left to right and from right to left to update the answer for each element arr[i] such that the answer for arr[i] is greater than the answers for arr[i+1] and arr[i – 1] if arr[i] is greater than arr[i + 1] and arr[i – 1]. Follow the below steps to solve the problem:
- Initialize a vector ans of size N that stores the minimum positive integer that can be assigned to each element.
- Initialize the vector ans with 1 as each element must be assigned some positive integer.
- Traverse the array from left to right using variable i and if arr[i] is greater than arr[i – 1] then update ans[i] as ans[i] = ans[i – 1] + 1.
- Now, traverse the array from right to left using variable i if arr[i] is greater than arr[i+1], then update ans[i] as ans[i] = max(ans[i], ans[i + 1] + 1).
- Now, find the sum of positive integers present in the vector ans and print it as the minimum possible sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minSum( int * arr, int n)
{
vector< int > ans(n, 1);
for ( int i = 1; i < n; i++) {
if (arr[i] > arr[i - 1]) {
ans[i] = max(ans[i],
ans[i - 1] + 1);
}
}
for ( int i = n - 2; i >= 0; i--) {
if (arr[i] > arr[i + 1]) {
ans[i] = max(ans[i],
ans[i + 1] + 1);
}
}
int s = 0;
for ( auto x : ans) {
s = s + x;
}
cout << s << endl;
}
int main()
{
int arr[] = { 1, 2, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
minSum(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void minSum( int [] arr,
int n)
{
int [] ans = new int [n];
Arrays.fill(ans, 1 );
for ( int i = 1 ; i < n; i++)
{
if (arr[i] > arr[i - 1 ])
{
ans[i] = Math.max(ans[i],
ans[i - 1 ] + 1 );
}
}
for ( int i = n - 2 ; i >= 0 ; i--)
{
if (arr[i] > arr[i + 1 ])
{
ans[i] = Math.max(ans[i],
ans[i + 1 ] + 1 );
}
}
int s = 0 ;
for ( int x : ans)
{
s = s + x;
}
System.out.print(s + "\n" );
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 2 };
int N = arr.length;
minSum(arr, N);
}
}
|
Python3
def minSum(arr, n):
ans = [ 1 ] * (n)
for i in range ( 1 , n):
if (arr[i] > arr[i - 1 ]):
ans[i] = max (ans[i],
ans[i - 1 ] + 1 )
for i in range (n - 2 ,
- 1 , - 1 ):
if (arr[i] > arr[i + 1 ]):
ans[i] = max (ans[i],
ans[i + 1 ] + 1 )
s = 0
for x in ans:
s = s + x
print (s)
if __name__ = = "__main__" :
arr = [ 1 , 2 , 2 ]
N = len (arr)
minSum(arr, N)
|
C#
using System;
class GFG{
static void minSum( int [] arr,
int n)
{
int [] ans = new int [n];
for ( int i = 0; i < n; i++)
ans[i] = 1;
for ( int i = 1; i < n; i++)
{
if (arr[i] > arr[i - 1])
{
ans[i] = Math.Max(ans[i],
ans[i - 1] + 1);
}
}
for ( int i = n - 2; i >= 0; i--)
{
if (arr[i] > arr[i + 1])
{
ans[i] = Math.Max(ans[i],
ans[i + 1] + 1);
}
}
int s = 0;
foreach ( int x in ans)
{
s = s + x;
}
Console.Write(s + "\n" );
}
public static void Main(String[] args)
{
int []arr = {1, 2, 2};
int N = arr.Length;
minSum(arr, N);
}
}
|
Javascript
<script>
function minSum(arr, n)
{
let ans = new Array(n).fill(1);
for (let i = 1; i < n; i++)
{
if (arr[i] > arr[i - 1])
{
ans[i] = Math.max(ans[i],
ans[i - 1] + 1);
}
}
for (let i = n - 2; i >= 0; i--)
{
if (arr[i] > arr[i + 1])
{
ans[i] = Math.max(ans[i],
ans[i + 1] + 1);
}
}
let s = 0;
for (let x in ans)
{
s = s + ans[x];
}
document.write(s + "\n" );
}
let arr = [1, 2, 2];
let N = arr.length;
minSum(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
06 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...