304 North Cardinal St.
Dorchester Center, MA 02124

# Maximize the minimal Array worth by altering components 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 may 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 aspect of arr[] in a single operation and might change it with its adjoining aspect.

Examples:

Enter: N = 7, Okay = 4, arr[] = {9, 7, 3, 5, 7, 8, 7}
Output: 7
Rationalization: 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
Rationalization: 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[] may be achieved by making use of operations is: 6

Strategy: To unravel the issue comply with the beneath concept:

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

Illustration with an Instance:

Take into account 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 aspect after making use of operation at most 3 instances is:  5

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

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

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

Observe 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 aspect on the final index of arr[].
• Else return the aspect 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` `major(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 House: O(1), as no additional area is required.