Understanding how much time and space an algorithm needs helps us write efficient programs. Let's break down both.
Time complexity is the amount of time taken by an algorithm to run, as a function of the size of its input.
int sum = 0; for (int i = 0; i < n; i++) { sum += i; }
This loop runs n
times ā Time Complexity: O(n)
Space complexity is the total memory space required by an algorithm, including input data, temporary variables, etc.
int arr[100];
This array takes space for 100 integers ā Space Complexity: O(1) (as size is fixed)
ā Tip: When designing an algorithm, always consider both time and space. Sometimes, we need to trade one for the other.
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!