Sunday, September 25, 2022
HomeSoftware DevelopmentMaximize the minimal Array worth by altering parts with adjoining Okay occasions

# Maximize the minimal Array worth by altering parts with adjoining Okay occasions

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 occasions.

• In a single operation it’s potential to pick any factor of arr[] in a single operation and may change it with its adjoining factor.

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

Method: To unravel the issue comply with the under concept:

Type the arr[], if Okay is bigger than or equal to size of arr[], merely return factor ultimately index of arr[] else return factor at Okayth index of arr[].

Illustration with an Instance:

Take into account N = 6, Okay = 3, arr[] = {9, 7, 3, 1, 2, 5}

We are able to 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 factor after making use of operation at most 3 occasions is:  5

When you’ll kind 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 resolve the issue:

• Type the array.
• Test if Okay is bigger than or equal to arr[] or not.
• If sure, then merely return the factor on the final index of arr[].
• Else return the factor on the Okayth index of arr[].
• Print the output.

Under is the implementation for the above strategy:

## Java

 ` `  `import` `java.util.*;` ` `  `class` `GFG {` ` `  `    ` `    ``public` `static` `void` `most important(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[])` `    `` X > arr.size)` `            ``return` `(arr[arr.length - ``1``]);` ` `  `        ` `        ` `        ` `        ``else` `            ``return` `(arr[X]);` `    ` `}`

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

RELATED ARTICLES