# Accepted Java solution converting range from inclusive to exclusive

• I use two modifications to the input before processing the array. This makes the number of cases to be handled inside the loop just one.

1. Make the range exclusive instead of inclusive

2. Create a new array with the range as a part of the input array

public List<String> findMissingRanges(int[] nums, int lower, int upper) {

``````  if (nums == null) return null;

List<String> res = new ArrayList<>();

int[] arr = new int[nums.length + 2];
//make range exclusive and add to the new input array
arr[0] = lower - 1;
arr[nums.length + 1] = upper + 1;
System.arraycopy(nums, 0, arr, 1, nums.length);

for (int i = 1; i < arr.length; i++)
if (arr[i] - arr[i - 1] > 1)
res.add((arr[i] - arr[i - 1] != 2) ? (arr[i - 1] + 1) + "->" + (arr[i] - 1) : (arr[i - 1] + 1) + "");

return res;
``````

}

Any improvements (other then using StringBuilder :))?

• Elegant although O(n) extra space seems waste just to avoid some edge cases.

• nice code. All you can improve is to add corner cases like upper limit is less than first element in the array or lower limit is greater than last element in the array. In such cases no need to scan the array.

• Nice and elegant solution! And here is mine without using O(n) extra memory.

``````public List<String> findMissingRanges(int[] nums, int lower, int upper) {

List<String> result = new ArrayList<String>();

if (nums == null || nums.length == 0){
result.add(upper == lower ? (lower + "") : (lower + "->" + upper));
return result;
}

for (int i = -1; i < nums.length; i++){
int next = i == nums.length - 1 ? upper + 1 : nums[i + 1];
int curr = i == -1 ? lower - 1 : nums[i];

if (next - curr != 1){
String str = next - curr == 2 ? ((curr + 1) + "") : ((curr + 1) + "->" + (next - 1));