0

I have started studying Data Structures and Algorithms just a few days ago and still trying to grasp the concepts. I was learning about Big-O notations. I understand what O(1)-Constant Time Complexity is and have question.

void Method1(int n) {
    int a = 10;
    int b = 20;        
    int x = a + n;
    int y = b * n;
    Console.Writeline("{0}{1}", x, y);
 }

The complexity of the above code is O(1) for a very large value of n. We are using value of N rather than doing processing N. Will the below method still have same complexity where n and m are very large numbers as inputs.

void Method1(int n, int m) {
        int a = 10;
        int b = 20;            
        int x = a + n;
        int y = b * m;
        Console.Writeline("{0}{1}", x, y);
     }
Hussain Patel
  • 460
  • 3
  • 10
  • 24

1 Answers1

0

This will have the same complexity with respect to the inputs, as you're doing the same operations. If you simply call your second method with the same value twice, it becomes identical to the first method.

Note, however, that certain implementations will not be O(1) for very large inputs. That's because "large integer" is a data type that expands with the magnitude of the input; those arithmetic operations are O(log N).

Prune
  • 76,765
  • 14
  • 60
  • 81