Skip to main content

Command Palette

Search for a command to run...

Minimum Average of Smallest and Largest Elements

Updated
2 min read
Minimum Average of Smallest and Largest Elements
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

You have an array of floating point numbers averages which is initially empty. You are given an array nums of n integers where n is even.

You repeat the following procedure n / 2 times:

  • Remove the smallest element, minElement, and the largest element maxElement, from nums.

  • Add (minElement + maxElement) / 2 to averages.

Return the minimum element in averages.

LeetCode Problem - 3194

import java.util.stream.Collectors;

class Solution {
    // Method to find the minimum average of pairs in the input array
    public double minimumAverage(int[] nums) {
        // Convert int array to List of Integers
        List<Integer> arr = Arrays.stream(nums).boxed().collect(Collectors.toList());

        // List to store average values of each pair
        List<Double> average = new ArrayList<>();
        int n = nums.length;

        // Iterate through half of the array to find pairs
        for(int i=0; i<n/2; i++){
            // Get array containing min and max elements from the list
            int[] check = returnMinAndMaxArrayElement(arr);
            // Calculate average of min and max elements
            double action = (double)(check[0]+check[1])/2;
            // Add average to the list
            average.add(action);
            // Remove min and max elements from the list
            arr.remove(Integer.valueOf(check[0]));
            arr.remove(Integer.valueOf(check[1]));
        }

        // Find the minimum average from the list of averages
        double minElement = Integer.MAX_VALUE;
        for(int i=0; i<average.size(); i++){
            if(average.get(i)<minElement){
                minElement = average.get(i);
            }
        }
        // Return the minimum average found
        return minElement;
    }

    // Method to return an array with minimum and maximum elements from the list
    public int[] returnMinAndMaxArrayElement(List<Integer> arr){
        // Initialize variables to store min and max elements
        int[] result = new int[2];
        int maxElement = Integer.MIN_VALUE;
        int minElement = Integer.MAX_VALUE;

        // Iterate through the list to find min and max elements
        for(int i=0; i<arr.size(); i++){
            if(arr.get(i)>maxElement){
                maxElement = arr.get(i);
            }
            if(arr.get(i) < minElement){
                minElement = arr.get(i);
            }
        }
        // Store min and max elements in the result array
        result[0] = minElement;
        result[1] = maxElement;
        // Return the result array containing min and max elements
        return result;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.