Find the Closest Min-Max using Javascript

The Min Max algorithm efficiently finds the minimum (min) and maximum (max) values in an array of numbers. This approach involves traversing the array while keeping track of the current minimum and maximum values encountered.

Problem Descript…


This content originally appeared on DEV Community and was authored by Krunal Kanojiya

The Min Max algorithm efficiently finds the minimum (min) and maximum (max) values in an array of numbers. This approach involves traversing the array while keeping track of the current minimum and maximum values encountered.

Problem Description

Given an array A, find the size of the smallest subarray such that it contains at least one occurrence of the maximum value of the array and at least one occurrence of the minimum value of the array.

Problem Constraints

1 <= |A| <= 2000

Input Format

First and only argument is vector A

Output Format

Return the length of the smallest subarray which has at least one occurrence of minimum and maximum element of the array.

Example Input / Output

Input 1:
A = [1, 3, 2]
Input 2:
A = [2, 6, 1, 6, 9]

Output 1:
2
Output 2:
3

Example Explanation

Explanation 1:
Take the 1st and 2nd elements as they are the minimum and maximum elements respectively.
Explanation 2:
Take the last 3 elements of the array.

JavaScript Output

function findMinMax(arr) {
    if (arr.length === 0) {
        return { min: null, max: null };
    }

    let min = arr[0];
    let max = arr[0];

    for (let i = 1; i < arr.length; i++) {
        if (arr[i] < min) {
            min = arr[i];
        }
        if (arr[i] > max) {
            max = arr[i];
        }
    }

    return { min: min, max: max };
}

// Example usage:
const array = [3, 5, 1, 8, 2, 9, -1];
const result = findMinMax(array);
console.log(`Min: ${result.min}, Max: ${result.max}`);  // Output: Min: -1, Max: 9


This content originally appeared on DEV Community and was authored by Krunal Kanojiya


Print Share Comment Cite Upload Translate Updates
APA

Krunal Kanojiya | Sciencx (2024-07-12T15:11:24+00:00) Find the Closest Min-Max using Javascript. Retrieved from https://www.scien.cx/2024/07/12/find-the-closest-min-max-using-javascript/

MLA
" » Find the Closest Min-Max using Javascript." Krunal Kanojiya | Sciencx - Friday July 12, 2024, https://www.scien.cx/2024/07/12/find-the-closest-min-max-using-javascript/
HARVARD
Krunal Kanojiya | Sciencx Friday July 12, 2024 » Find the Closest Min-Max using Javascript., viewed ,<https://www.scien.cx/2024/07/12/find-the-closest-min-max-using-javascript/>
VANCOUVER
Krunal Kanojiya | Sciencx - » Find the Closest Min-Max using Javascript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/12/find-the-closest-min-max-using-javascript/
CHICAGO
" » Find the Closest Min-Max using Javascript." Krunal Kanojiya | Sciencx - Accessed . https://www.scien.cx/2024/07/12/find-the-closest-min-max-using-javascript/
IEEE
" » Find the Closest Min-Max using Javascript." Krunal Kanojiya | Sciencx [Online]. Available: https://www.scien.cx/2024/07/12/find-the-closest-min-max-using-javascript/. [Accessed: ]
rf:citation
» Find the Closest Min-Max using Javascript | Krunal Kanojiya | Sciencx | https://www.scien.cx/2024/07/12/find-the-closest-min-max-using-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.