One-dimensional arrays in C - Time & Space Complexity
When working with one-dimensional arrays, it is important to understand how the time to process them grows as the array gets bigger.
We want to know how the number of steps changes when the array size increases.
Analyze the time complexity of the following code snippet.
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d\n", arr[i]);
}
}
This code prints each element of a one-dimensional array of size n.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing and printing each element of the array.
- How many times: Exactly once for each element, so n times.
As the array size grows, the number of steps grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: Doubling the array size doubles the work needed.
Time Complexity: O(n)
This means the time to print the array grows directly with the number of elements.
[X] Wrong: "Accessing an array element takes more time as the array grows."
[OK] Correct: Each element access is done in constant time, no matter the array size. The total time grows only because there are more elements to access.
Understanding how simple loops over arrays scale is a key skill. It helps you explain and reason about code efficiency clearly and confidently.
"What if we nested another loop inside to compare each element with every other? How would the time complexity change?"
Practice
Solution
Step 1: Understand array indexing in C
In C, arrays always start indexing from 0, meaning the first element is at index 0.Step 2: Confirm the starting index
This is a fixed rule in C and does not depend on array size or other factors.Final Answer:
0 -> Option AQuick Check:
Array indexing starts at 0 [OK]
- Thinking arrays start at 1 like in some other languages
- Assuming negative indexes are valid
- Believing index depends on array size
Solution
Step 1: Recall C array declaration syntax
In C, arrays are declared by specifying the type, name, and size in square brackets, like int arr[5];Step 2: Check each option
int arr[5]; matches the correct syntax. int arr(); uses parentheses which is for functions. int arr = 5; assigns a single int, not an array. array int arr[5]; uses invalid keyword 'array'.Final Answer:
int arr[5]; -> Option DQuick Check:
Array declaration uses square brackets [OK]
- Using parentheses instead of square brackets
- Assigning a single value instead of declaring array
- Using invalid keywords like 'array'
int arr[3] = {10, 20, 30};
printf("%d", arr[1]);Solution
Step 1: Identify the array elements and indexes
The array arr has elements: arr[0]=10, arr[1]=20, arr[2]=30.Step 2: Understand the printf statement
printf prints the value at arr[1], which is 20.Final Answer:
20 -> Option CQuick Check:
arr[1] = 20 [OK]
- Confusing index 1 with index 0
- Expecting output 10 or 30 incorrectly
- Thinking code causes compilation error
int arr[4]; arr[4] = 10;
Solution
Step 1: Check array size and indexing
The array arr has size 4, so valid indexes are 0,1,2,3 only.Step 2: Identify the invalid index usage
arr[4] accesses the fifth element, which is outside the array bounds causing undefined behavior.Final Answer:
Array index out of bounds -> Option AQuick Check:
Index must be less than array size [OK]
- Using index equal to array size
- Ignoring zero-based indexing
- Assuming no error for out-of-bounds access
int nums[5] = {1, 2, 3, 4, 5};, which code snippet correctly calculates the sum of all elements?Solution
Step 1: Understand array size and valid indexes
The array nums has 5 elements with indexes 0 to 4.Step 2: Analyze each loop condition
int sum = 0; for(int i = 0; i <= 5; i++) sum += nums[i]; uses i <= 5 which accesses nums[5] (out of bounds). int sum = 0; for(int i = 0; i < 5; i++) sum += nums[i]; uses i < 5 which correctly iterates from 0 to 4. int sum = 0; for(int i = 1; i < 5; i++) sum += nums[i]; starts at 1, missing nums[0]. int sum = 0; for(int i = 0; i < 4; i++) sum += nums[i]; stops at 4, missing nums[4].Final Answer:
int sum = 0; for(int i = 0; i < 5; i++) sum += nums[i]; -> Option BQuick Check:
Loop from 0 to less than size sums all elements [OK]
- Using <= size causing out-of-bounds access
- Starting loop at 1 missing first element
- Stopping loop early missing last element
Ready to prove it?
Pass the test and this pattern moves unknown
