Programmers / Remove Smallest Number

Programmers / Remove Smallest Number

Problem

Solution 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
    public int[] solution(int[] arr) {
        // Check array size and return -1
        if (arr.length == 0 || arr.length == 1) {
            return new int[] {-1};
        }
        
        // Find min value
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < arr.length; i++) {
            if (min > arr[i]) {
                min = arr[i];
            }
        }
        
        // Set result
        int j = 0;
        int[] result = new int[arr.length-1];
        for (int i = 0; i < arr.length; i++) {
            if (min != arr[i]) {
                result[j] = arr[i];
                j++;
            }
        }
        
        return result;
    }
}
Solution 1
  • Description
    • First find the smallest value, then remove it from the array
  • Time Complexity
    • O(len(arr))
    • Two for loops of size len(arr)
  • Space Complexity
    • O(len(arr))
    • Memory usage proportional to len(arr) for function input