[LeetCode] Neither Minimum nor Maximum

2733. Neither Minimum nor Maximum

Given an integer array nums containing distinct positive integers, find and return any number from the array that is neither the minimum nor the maximum value in the array, or -1 if there is no such number.

Return the selected integer.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
int findNonMinOrMax(vector<int>& nums) {
sort(begin(nums), end(nums));
for(int i = 0; i < nums.size(); i++) {
if(nums[i] == nums[0] or nums[i] == nums.back()) continue;
return nums[i];
}
return -1;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2023/06/11/PS/LeetCode/neither-minimum-nor-maximum/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.