Open In App

Maximize indices with value more than sum of neighbours

Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ Algorithm of the above approach
#include <iostream>
#include <vector>
using namespace std;
 
// Function which returns maximum number of indices
int highestVal(vector<int> arr, int K)
{
    int res = 0;
    int N = arr.size();
 
    // Case-1 K = 1
    if (K == 1) {
        int even = N / 2 + 1;
        res = N - even;
    }
 
    // Case-2 Other values of K
    else {
        for (int i = 1; i < N - 1; i++) {
            if (arr[i] > arr[i - 1] + arr[i + 1])
                res++;
        }
    }
 
    // Returning number of indices
    return res;
}
 
// Driver Code
int main()
{
    vector<int> arr = { 2, 9, 2, 4, 1 };
    int K = 2;
   
    // Function call
    int ans = highestVal(arr, K);
    cout << ans << endl;
    return 0;
}
 
// This code is contributed by Kdheeraj


Java




// Java algorithm of the above approach
 
import java.util.*;
 
class GFG {
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 2, 9, 2, 4, 1 };
        int K = 2;
 
        // Function call
        int ans = highestVal(arr, K);
        System.out.println(ans);
    }
 
    // Function which returns maximum number of indices
    public static int highestVal(int[] arr, int K)
    {
        int res = 0;
        int N = arr.length;
 
        // Case-1 K = 1
        if (K == 1) {
            int even = N / 2 + 1;
            res = N - even;
        }
 
        // Case-2 Other values of K
        else {
            for (int i = 1; i < N - 1; i++) {
                if (arr[i] > arr[i - 1] + arr[i + 1])
                    res++;
            }
        }
       
        // Returning number of indices
        return res;
    }
}


Python3




# Python3 Algorithm of the above approach
 
# Function which returns maximum number of indices
def highestVal(arr, K) :
 
    res = 0;
    N = len(arr);
 
    # Case-1 K = 1
    if (K == 1) :
        even = N // 2 + 1;
        res = N - even;
 
    # Case-2 Other values of K
    else :
        for i in range(1, N - 1) :
            if (arr[i] > arr[i - 1] + arr[i + 1]) :
                res += 1;
 
    # Returning number of indices
    return res;
 
# Driver Code
if __name__ == "__main__" :
 
    arr = [ 2, 9, 2, 4, 1 ];
    K = 2;
   
    # Function call
    ans = highestVal(arr, K);
    print(ans);
 
    # This code is contributed by AnkThon


C#




// C# algorithm of the above approach
 
using System;
 
public class GFG {
 
  static public void Main()
  {
 
    // Code
    int[] arr = { 2, 9, 2, 4, 1 };
    int K = 2;
 
    // Function call
    int ans = highestVal(arr, K);
    Console.WriteLine(ans);
  }
 
  // Function which returns maximum number of indices
  public static int highestVal(int[] arr, int K)
  {
    int res = 0;
    int N = arr.Length;
 
    // Case-1 K = 1
    if (K == 1) {
      int even = N / 2 + 1;
      res = N - even;
    }
 
    // Case-2 Other values of K
    else {
      for (int i = 1; i < N - 1; i++) {
        if (arr[i] > arr[i - 1] + arr[i + 1])
          res++;
      }
    }
 
    // Returning number of indices
    return res;
  }
}
 
// This code is contributed by lokesh (lokeshmvs21).


Javascript




<script>
 
// Javascript  Algorithm of the above approach
 
// Function which returns maximum number of indices
function highestVal(arr,K)
{
    let res = 0;
    let N = arr.length;
 
    // Case-1 K = 1
    if (K == 1) {
        let even = N / 2 + 1;
        res = N - even;
    }
 
    // Case-2 Other values of K
    else {
        for (let i = 1; i < N - 1; i++) {
            if (arr[i] > arr[i - 1] + arr[i + 1])
                res++;
        }
    }
 
    // Returning number of indices
    return res;
}
 
// Driver Code
 
    let  arr = [ 2, 9, 2, 4, 1 ];
    let K = 2;
   
    // Function call
    let  ans = highestVal(arr, K);
    document.write(ans) ;
     
    // This code is contributed by satwik4409.
   </script>


Output

2

Time Complexity: O(N)
Auxiliary Space: O(1)



Last Updated : 28 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads