0
public static void main(String[] args){
    int n;
    Scanner in = new Scanner(System.in);
    n=in.nextInt();
    int[] arr = new int[n];
    for(int i=0; i<=n; i++){
        arr[i]=i*2;
    }
}

I'm just confused about what is the input in space or time complexity.

hamal
  • 55
  • 6
  • This is a strange example, technically the space and time complexity of the main method is `O(1)` because `n=5`. If you were to create a new method, `foo(int n)`, that would change because there is actually an input. – Clashsoft Jun 05 '21 at 14:58
  • @Clashsoft modified the code a bit. what is the auxiliary and input space here then? – hamal Jun 05 '21 at 15:03
  • 1
    How much input are you giving the code? Only the value `n`, so input space is _O(1)_. --- How much space does the code use? The array `arr`, so auxiliary space is _O(n)_, resulting in a space complexity of _O(n)_. --- How much time is used? Iterating the array once, so time complexity is _O(n)_. – Andreas Jun 05 '21 at 15:24
  • *FYI:* Code will fail. Change `i<=n` to `i – Andreas Jun 05 '21 at 15:26
  • @Andreas could you give a detailed answer to the modified question? Thanks in advance. – hamal Jun 05 '21 at 16:27
  • Possible reference: https://stackoverflow.com/a/60846485/11811255 – jizhihaoSAMA Jun 05 '21 at 16:33
  • @jizhihaoSAMA nah, didn't help! – hamal Jun 05 '21 at 16:35
  • Firstly, Your code is incomplete. The mentioned answer: **This is a matter of convention. It is important what n denotes in a particular problem.**. In one word, people could define the mean of `N` by himself. – jizhihaoSAMA Jun 05 '21 at 16:39
  • @jizhihaoSAMA please consider 'n' as a user input integer. – hamal Jun 05 '21 at 16:40

0 Answers0