Leetcode 2733: Neither Minimum nor Maximum

grid47
grid47
Exploring patterns and algorithms
Feb 7, 2024 5 min read

You are given an array nums containing distinct positive integers. Your task is to find and return any number from the array that is neither the smallest nor the largest number, or return -1 if no such number exists.
Problem
Approach
Steps
Complexity
Input: The input consists of a single array `nums` of integers.
Example: nums = [4, 2, 1, 3]
Constraints:
• 1 <= nums.length <= 100
• 1 <= nums[i] <= 100
• All values in nums are distinct.
Output: Return any integer from the array that is neither the minimum nor the maximum value, or return -1 if no such number exists.
Example: Output: 2
Constraints:
Goal: The goal is to find an integer from the array that is neither the smallest nor the largest number.
Steps:
• Check if the array has less than 3 elements. If true, return -1.
• Find the minimum and maximum values in the array.
• Loop through the array and return any number that is not the minimum or maximum.
Goal: The length of the array and the range of the integers in the array.
Steps:
• 1 <= nums.length <= 100
• 1 <= nums[i] <= 100
• All values in nums are distinct.
Assumptions:
• All integers in the array are distinct.
• The array length is at least 2.
Input: nums = [4, 2, 1, 3]
Explanation: In this example, the minimum value is 1 and the maximum value is 4. Therefore, 2 or 3 can be valid answers.

Input: nums = [1, 2]
Explanation: Since the array has only two elements, no element is both non-minimal and non-maximal. Therefore, the output is -1.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus