Maximize the minimal Array worth by altering parts with adjoining Okay instances


Given an array arr[] of N integers and an integer Okay, the place Okay denotes the utmost variety of operations which will be utilized to the array, the duty is to maximise the minimal worth of arr[] through the use of the given operation at most Okay instances.

  • In a single operation it’s attainable to pick any ingredient of arr[] in a single operation and might change it with its adjoining ingredient.

Examples:

Enter: N = 7, Okay = 4, arr[] = {9, 7, 3, 5, 7, 8, 7}
Output: 7
Clarification: First operation: Change 3 at index 2 with 7 at index 1. 
So the arr[] turns into: {9, 7, 7, 5, 7, 8, 7}
Second Operation: Change 5 at index 3 with 7 at index 2.
So the arr[] turns into: {9, 7, 7, 7, 7, 8, 7} 
Third operation: Change 7 at index 6 with 8 at index 5.
So the arr[] turns into: {9, 7, 7, 7, 7, 8, 8}
Fourth Operation: Change 7 at index 1 with 9 at index 0.
So the arr[] turns into: {9, 9, 7, 7, 7, 8, 8}
The minimal worth in arr[] after making use of operation at most Okay instances is: 7

Enter: N = 4, Okay = 2, arr[] = {2, 5, 6, 8}
Output: 6
Clarification: First operation: Change 5 at index 1 with 6 at index 2.
In order that the arr[] turns into: {2, 6, 6, 8}
Second operation: Change 2 at index 0 with 6 at index 1.
In order that the arr[] turns into: {6, 6, 6, 8}
The minimal worth of arr[] will be achieved by making use of operations is: 6

Method: To unravel the issue observe the under concept:

Kind the arr[], if Okay is larger than or equal to size of arr[], merely return ingredient finally index of arr[] else return ingredient at Okayth index of arr[]. 

Illustration with an Instance:

Think about N = 6, Okay = 3, arr[] = {9, 7, 3, 1, 2, 5}

We will carry out the next operations

Operation 1:- Change 2 at index 4 with 5 at index 5 . So the arr[] turns into: {9, 7, 3, 1, 5, 5}
Operation 2:- Change 1 at index 3 with 5 at index 4 . So the arr[] turns into: {9, 7, 3, 5, 5, 5}
Operation 3:- Change 3 at index 2 with 7 at index 1 . So the arr[] turns into: {9, 7, 7, 5, 5, 5}
Minimal ingredient after making use of operation at most 3 instances is:  5

When you’ll type the arr[] and return arr[K] you’re going to get the identical output :-

Sorted arr[]: {1, 2, 3, 5, 7, 9}

arr[K] = arr[3] = 5, which is out required reply.  

Comply with the steps to unravel the issue:

  • Kind the array.
  • Verify if Okay is larger than or equal to arr[] or not. 
    • If sure, then merely return the ingredient on the final index of arr[].
    • Else return the ingredient on the Okayth index of arr[].
  • Print the output.

Under is the implementation for the above method:

Java

  

import java.util.*;

  

class GFG {

  

    

    public static void essential(String[] args)

    {

        int N = 6, X = 3;

        int[] arr = { 9, 7, 3, 1, 2, 5 };

  

        

        System.out.println(Min_Value(N, X, arr));

    }

  

    

    static int Min_Value(int N, int X, int arr[])

    

}

Time Complexity: O(N * logN), as a result of sorting is carried out.
Auxiliary Area: O(1), as no additional area is required.

Similar Posts

Leave a Reply

Your email address will not be published.