Maximize indices with value more than sum of neighbours
Given an array of integers arr[], the task is to find the maximum number of the indices having values greater than the sum of the neighbours after performing the given operation:
- In an operation, you can select K(given) consecutive elements and increase their values by 1.
- This operation can be performed any number of times
Examples:
Input: arr[] = [2, 9, 2, 4, 1], K = 2
Output: 2
Explanation: Here arr[1] and arr[3] is fulfilling the criteria.
No matter how many times you increase K consecutive elements,
you cannot get more than 2 such indices.
Input: arr[] = [3, 1, 3, 4, 5, 1, 2], K = 1
Output: 3
Explanation: We will select K =1 consecutive elements and increment it’s value by 1
Select arr[1] six times then array becomes: [3, 7, 3, 4, 5, 1, 2]
Select arr[3] five times then array becomes: [3, 7, 3, 9, 5, 1, 2]
Select arr[5] seven times then array becomes: [3, 7, 3, 9, 5, 8, 2]
Now arr[1], arr[3] and arr[5] satisfies the criteria.
Approach: To solve the problem follow the below idea:
This problem deals with finding the maximum count of indices, after performing the given operation any number of times.
Here, if K = 1, then the answer will be equal to the number of elements present at the odd index,
If K > 1 then the answer will be equal to the number of indices that already satisfies the given criteria, as increasing the values of a subarray can never help to get a new index, as the values of its neighbors will also increase.
Follow the given steps to solve the problem:
- If K = 1, then the answer will be the number of elements present at an odd index in arr[]
- For other values of K, here the answer will be those indices whose arr[i] > arr[i + 1] + arr[i – 1].
- Return the count depending on the value of K.
Below is the implementation of the above approach.
C++
#include <iostream>
#include <vector>
using namespace std;
int highestVal(vector< int > arr, int K)
{
int res = 0;
int N = arr.size();
if (K == 1) {
int even = N / 2 + 1;
res = N - even;
}
else {
for ( int i = 1; i < N - 1; i++) {
if (arr[i] > arr[i - 1] + arr[i + 1])
res++;
}
}
return res;
}
int main()
{
vector< int > arr = { 2, 9, 2, 4, 1 };
int K = 2;
int ans = highestVal(arr, K);
cout << ans << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
int [] arr = { 2 , 9 , 2 , 4 , 1 };
int K = 2 ;
int ans = highestVal(arr, K);
System.out.println(ans);
}
public static int highestVal( int [] arr, int K)
{
int res = 0 ;
int N = arr.length;
if (K == 1 ) {
int even = N / 2 + 1 ;
res = N - even;
}
else {
for ( int i = 1 ; i < N - 1 ; i++) {
if (arr[i] > arr[i - 1 ] + arr[i + 1 ])
res++;
}
}
return res;
}
}
|
Python3
def highestVal(arr, K) :
res = 0 ;
N = len (arr);
if (K = = 1 ) :
even = N / / 2 + 1 ;
res = N - even;
else :
for i in range ( 1 , N - 1 ) :
if (arr[i] > arr[i - 1 ] + arr[i + 1 ]) :
res + = 1 ;
return res;
if __name__ = = "__main__" :
arr = [ 2 , 9 , 2 , 4 , 1 ];
K = 2 ;
ans = highestVal(arr, K);
print (ans);
|
C#
using System;
public class GFG {
static public void Main()
{
int [] arr = { 2, 9, 2, 4, 1 };
int K = 2;
int ans = highestVal(arr, K);
Console.WriteLine(ans);
}
public static int highestVal( int [] arr, int K)
{
int res = 0;
int N = arr.Length;
if (K == 1) {
int even = N / 2 + 1;
res = N - even;
}
else {
for ( int i = 1; i < N - 1; i++) {
if (arr[i] > arr[i - 1] + arr[i + 1])
res++;
}
}
return res;
}
}
|
Javascript
<script>
function highestVal(arr,K)
{
let res = 0;
let N = arr.length;
if (K == 1) {
let even = N / 2 + 1;
res = N - even;
}
else {
for (let i = 1; i < N - 1; i++) {
if (arr[i] > arr[i - 1] + arr[i + 1])
res++;
}
}
return res;
}
let arr = [ 2, 9, 2, 4, 1 ];
let K = 2;
let ans = highestVal(arr, K);
document.write(ans) ;
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
28 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...