LeetCode-in-Java

3912. Valid Elements in an Array

Easy

You are given an integer array nums.

An element nums[i] is considered valid if it satisfies at least one of the following conditions:

The first and last elements are always valid.

Return an array of all valid elements in the same order as they appear in nums.

Example 1:

Input: nums = [1,2,4,2,3,2]

Output: [1,2,4,3,2]

Explanation:

Example 2:

Input: nums = [5,5,5,5]

Output: [5,5]

Explanation:

Example 3:

Input: nums = [1]

Output: [1]

Explanation:

Since there is only one element, it is always valid. Thus, the answer is [1].

Constraints:

Solution

import java.util.ArrayList;
import java.util.List;

public class Solution {
    public List<Integer> findValidElements(int[] nums) {
        List<Integer> ans = new ArrayList<>();
        int n = nums.length;
        ans.add(nums[0]);
        for (int i = 1; i < n - 1; i++) {
            boolean left = true;
            boolean right = true;
            for (int j = 0; j < i; j++) {
                if (nums[i] <= nums[j]) {
                    left = false;
                    break;
                }
            }
            for (int j = i + 1; j < n; j++) {
                if (nums[i] <= nums[j]) {
                    right = false;
                    break;
                }
            }
            if (left || right) {
                ans.add(nums[i]);
            }
        }
        if (n > 1) {
            ans.add(nums[n - 1]);
        }
        return ans;
    }
}