Hi all,

This is to do with the time and space complexity of the code below, an exercise on an algorithms course. It asks the user to choose integer n, then give integers between 1 and n minus one of the integers. Then it finds which of the integers is missing. The code as it is works just fine, however it runs too slow. The exercise instructions give as a hint that there is an algorithm for this with time complexity of O(n) and space complexity of O(1) and that the code is to be able to handle a million integers in a few seconds. Note that I can only add/alter things within the missingNumber( ) method. I'm a bit of a beginner in the time and space complexity subject matter, so could someone give me some hints as to which direction to go to improve my code? Thanks a bunch.

import java.util.Scanner;
 
public class MissingNumber {
 
    public static Scanner reader = new Scanner(System.in);
 
    public static int missingNumber(int[] numbers) {
        int missing = 0;
 
        for (int i: numbers) {
           missing ^= i;
        }
 
        for (int i = 1; i <= numbers.length + 1; i++) {
            missing ^= i;
        }
 
        return missing;
    }
 
    public static void main(String[] args) {
        System.out.print("Max number? ");
        int biggest = reader.nextInt();
        int numbers[] = new int[biggest - 1];
 
        System.out.println("Give numbers:");
 
        for (int i = 0; i < biggest - 1; i++) {
            numbers[i] = reader.nextInt();
        }
 
        int missing = missingNumber(numbers);
 
        System.out.println("Missing number: " + missing);
    }
}