Friday, September 9, 2022
HomeWordPress DevelopmentMaximize the minimal Array worth by altering components with adjoining Okay occasions

Maximize the minimal Array worth by altering components 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 might be utilized to the array, the duty is to maximise the minimal worth of arr[] by utilizing the given operation at most Okay occasions.

  • In a single operation it’s doable 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 occasions 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[] might be achieved by making use of operations is: 6

Strategy: To resolve the issue observe the under thought:

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

Illustration with an Instance:

Contemplate 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 occasions 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[3] = 5, which is out required reply.  

Comply with the steps to resolve the issue:

  • Kind the array.
  • Test 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.

Beneath is the implementation for the above strategy:

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[])

     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

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments