Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Maximize value obtained in Array by jumping to the next consecutive greater

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Given an array arr[] of size N, the task is to find the maximum value that can be obtained by following the below conditions:

  • Select any element (say k) from the array and increase all other elements by 1.
  • In the next step, jump only to the index with a value k+1.
  • In the end, you are at the maximum value.

Examples:

Input: N = 4, arr[] ={1, 2, 1, 3}
Output: 3
Explanation: If started from index 0 with a height of 1 unit, then,  
the new value of array will be [1, 3, 2, 4]. 
Then jump to the index with (1+1 = 2) ie 2nd index,  
The updated values are [2, 4, 2, 5]. Cannot be at the maximum value at end
The first chosen value was 3 at index 3. 
The updated values are [2, 3, 2, 3]. Max achieved -3. Hence ans = 3;

Input: N = 4, arr[]={1, 2, 3, 3}
Output: 4

 

Approach: The problem can be solved based on the following observation:

On observation, we can realize that for reaching maximum height we have two options

  • Directly selecting the maximum heighted podium initially available.
  • Choosing all the elements (say total y) with same value (say x). So highest number that can be reached is (x + y – 1).

Follow the below steps to solve the problem:

  • Sort the array in increasing order.
  • Seek for the span of the same value elements and get the maximum value that can be achieved from that span using the above idea.
  • Perform this for all available spans and store the maximum.
  • Return the maximum as the required answer.

Below is the implementation of the above approach.

C++




// C++ code to implement the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the largest value
int maxVal(int n, int a[])
{
    // Sort to get highest span and
    // maximum initial value
    sort(a, a + n);
    int ans = 0, span = 0;
    for (int i = 1; i < n; i++) {
 
        // Increase the span if they are same
        if (a[i - 1] == a[i]) {
            span++;
        }
        else {
 
            // ans updation if
            // new value is bigger
            ans = max(ans, a[i - 1] + span);
            span = 0;
        }
    }
    ans = max(ans, a[n - 1] + span);
    ans = max(ans, a[n - 1]);
 
    // Checking max of new ans
    // and initial maximum
    return ans;
}
 
// Driver Code
int main()
{
    int N = 4;
    int arr[] = { 1, 2, 1, 3 };
 
    // Function call
    cout << maxVal(N, arr) << endl;
    return 0;
}

Java




// Java code to implement the approach
import java.io.*;
import java.util.*;
 
class GFG
{
 
  // Function to find the largest value
  public static int maxVal(int n, int a[])
  {
 
    // Sort to get highest span and
    // maximum initial value
    Arrays.sort(a);
    int ans = 0, span = 0;
    for (int i = 1; i < n; i++) {
 
      // Increase the span if they are same
      if (a[i - 1] == a[i]) {
        span++;
      }
      else {
 
        // ans updation if
        // new value is bigger
        ans = Math.max(ans, a[i - 1] + span);
        span = 0;
      }
    }
    ans = Math.max(ans, a[n - 1] + span);
    ans = Math.max(ans, a[n - 1]);
 
    // Checking max of new ans
    // and initial maximum
    return ans;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 4;
    int arr[] = { 1, 2, 1, 3 };
 
    // Function call
    System.out.println(maxVal(N, arr));
  }
}
 
// This code is contributed by Rohit Pradhan

Python3




# Python code to implement the approach
 
# Function to find the largest value
def maxVal(n, a):
   
    # Sort to get highest span and
    # maximum initial value
    a.sort()
    ans = 0
    span = 0
    for i in range(1, n):
 
        # Increase the span if they are same
        if (a[i - 1] == a[i]):
            span += 1
        else:
 
            # ans updation if
            # new value is bigger
            ans = max(ans, a[i - 1] + span)
            span = 0
 
    ans = max(ans, a[n - 1] + span)
    ans = max(ans, a[n - 1])
 
    # Checking max of new ans
    # and initial maximum
    return ans
 
# Driver Code
N = 4
arr = [1, 2, 1, 3]
 
# Function call
print(maxVal(N, arr))
 
# This code is contributed by gfgking.

C#




// C# program to implement
// the above approach
using System;
 
class GFG
{
   
  // Function to find the largest value
  public static int maxVal(int n, int[] a)
  {
 
    // Sort to get highest span and
    // maximum initial value
    Array.Sort(a);
    int ans = 0, span = 0;
    for (int i = 1; i < n; i++) {
 
      // Increase the span if they are same
      if (a[i - 1] == a[i]) {
        span++;
      }
      else {
 
        // ans updation if
        // new value is bigger
        ans = Math.Max(ans, a[i - 1] + span);
        span = 0;
      }
    }
    ans = Math.Max(ans, a[n - 1] + span);
    ans = Math.Max(ans, a[n - 1]);
 
    // Checking max of new ans
    // and initial maximum
    return ans;
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 4;
    int[] arr = { 1, 2, 1, 3 };
 
    // Function call
    Console.Write(maxVal(N, arr));
  }
}
 
// This code is contributed by shinjanpatra

Javascript




<script>
 
// JavaScript code to implement the approach
 
// Function to find the largest value
function maxVal(n, a)
{
    // Sort to get highest span and
    // maximum initial value
    a.sort();
    let ans = 0, span = 0;
    for (let i = 1; i < n; i++) {
 
        // Increase the span if they are same
        if (a[i - 1] == a[i]) {
            span++;
        }
        else {
 
            // ans updation if
            // new value is bigger
            ans = Math.max(ans, a[i - 1] + span);
            span = 0;
        }
    }
    ans = Math.max(ans, a[n - 1] + span);
    ans = Math.max(ans, a[n - 1]);
 
    // Checking max of new ans
    // and initial maximum
    return ans;
}
 
// Driver Code
 
let N = 4;
let arr = [ 1, 2, 1, 3 ];
 
// Function call
document.write(maxVal(N, arr),"</br>");
 
// This code is contributed by shinjanpatra
 
</script>

Output

3

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


My Personal Notes arrow_drop_up
Last Updated : 06 Jun, 2022
Like Article
Save Article
Similar Reads
Related Tutorials