First and Last Occurrence of Element in DSA C - Time & Space Complexity
We want to know how long it takes to find the first and last positions of a number in a list.
How does the time needed change when the list gets bigger?
Analyze the time complexity of the following code snippet.
int firstOccurrence(int arr[], int n, int x) {
for (int i = 0; i < n; i++) {
if (arr[i] == x) return i;
}
return -1;
}
int lastOccurrence(int arr[], int n, int x) {
for (int i = n - 1; i >= 0; i--) {
if (arr[i] == x) return i;
}
return -1;
}
This code finds the first and last places where a number appears in an array by checking elements one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the array elements one by one.
- How many times: Up to n times in each function, where n is the array size.
As the list gets bigger, the number of checks grows roughly the same as the list size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The work grows directly with the size of the list.
Time Complexity: O(n)
This means the time to find the first or last occurrence grows in a straight line as the list gets bigger.
[X] Wrong: "Since the array is sorted, we can find the first and last occurrence instantly without checking many elements."
[OK] Correct: Without using a special method like binary search, this code checks elements one by one, so it still takes time proportional to the list size.
Understanding how searching through a list grows with size helps you explain your approach clearly and shows you know how to think about efficiency.
"What if we used binary search to find the first and last occurrence in a sorted array? How would the time complexity change?"
Practice
first occurrence of an element in an array mean?Solution
Step 1: Understand the meaning of first occurrence
The first occurrence means the earliest position where the element is found in the array, starting from index 0.Step 2: Compare options with definition
The index of the first time the element appears in the array matches this definition exactly, while others describe different concepts.Final Answer:
The index of the first time the element appears in the array -> Option CQuick Check:
First occurrence = first index found [OK]
- Confusing first occurrence with count of element
- Mixing first occurrence with last occurrence
- Thinking it means sum or total
x in array arr of size n?Solution
Step 1: Analyze each code snippet
for(int i=0; iStep 2: Check other options for correctness
for(int i=n-1; i>=0; i--) { if(arr[i] == x) return i; } return -1; searches from end, so it finds last occurrence. int i=0; while(arr[i] != x) i++; return i; may cause out-of-bound error if x not found. for(int i=0; iFinal Answer:
for(int i=0; i -> Option AQuick Check:
Start to end loop with match check = for(int i=0; i
- Looping backward for first occurrence
- Not handling element not found case
- Returning wrong index when element differs
int arr[] = {2, 4, 6, 4, 8};
int n = 5, x = 4;
int first = -1, last = -1;
for(int i=0; iSolution
Step 1: Trace the loop to find first and last occurrence
Array: [2,4,6,4,8], x=4. Loop i=0 to 4:
i=0: arr[0]=2 !=4, skip
i=1: arr[1]=4 ==4, first=-1 so first=1, last=1
i=2: arr[2]=6 !=4, skip
i=3: arr[3]=4 ==4, first=1 (unchanged), last=3
i=4: arr[4]=8 !=4, skipStep 2: Print first and last
first=1, last=3Final Answer:
1 3 -> Option AQuick Check:
First=1, Last=3 indexes for 4 [OK]
- Swapping first and last values
- Not updating last occurrence
- Ignoring first occurrence initialization
x in array arr of size n:int lastOccurrence(int arr[], int n, int x) {
int last = -1;
for(int i=0; iSolution
Step 1: Check the if condition inside the loop
The condition uses single '=' which is assignment, not comparison. This causes wrong behavior.Step 2: Correct the condition
It should be 'if(arr[i] == x)' to compare values properly.Final Answer:
Using assignment (=) instead of comparison (==) in if condition -> Option BQuick Check:
Use '==' for comparison, not '=' [OK]
- Using '=' instead of '==' in conditions
- Loop direction confusion for last occurrence
- Wrong initialization of variables
arr of size n with possible duplicates, which approach is the most efficient to find the first and last occurrence of element x?Solution
Step 1: Understand the array is sorted
Since the array is sorted, binary search can be used to find positions efficiently.Step 2: Use binary search twice
One binary search finds the first occurrence by moving left on matches, another finds last occurrence by moving right on matches.Final Answer:
Use two binary searches: one for first occurrence, one for last occurrence -> Option DQuick Check:
Sorted array + binary search = fastest method [OK]
- Using linear search on sorted array (slow)
- Sorting array again unnecessarily
- Not using binary search for efficiency
Ready to prove it?
Pass the test and this pattern moves unknown
