📖 Scenario: You have a list of numbers representing daily temperatures recorded over a week. You want to find the first and last day a specific temperature was recorded.
🎯 Goal: Build a program that finds the first and last occurrence of a given temperature in the list.
📋 What You'll Learn
Create an array with exact temperature values
Create a variable for the target temperature to search
Use a loop to find the first and last occurrence indexes
Print the first and last occurrence indexes
💡 Why This Matters
🌍 Real World
Finding first and last occurrences is useful in data analysis, like detecting when events start and end in a timeline.
💼 Career
This skill helps in debugging, searching logs, and processing data arrays in software development.
Progress0 / 4 steps
1
Create the temperature array
Create an integer array called temps with these exact values: 23, 25, 23, 22, 25, 23, 24 and an integer size set to 7.
DSA C
Hint
Use curly braces to list all 7 temperature values inside the array.
2
Set the target temperature
Create an integer variable called target and set it to 23.
DSA C
Hint
Just create a variable named target and assign it the value 23.
3
Find first and last occurrence
Create two integer variables first and last and set both to -1. Use a for loop with variable i from 0 to size - 1 to check if temps[i] equals target. If yes, set first to i if first is -1, and always update last to i.
DSA C
Hint
Use a loop from 0 to size-1. Check each element. Set first only once, update last every time you find the target.
4
Print the results
Use printf to print the first and last occurrence indexes in this exact format: First occurrence: X and Last occurrence: Y, where X and Y are the values of first and last.
DSA C
Hint
Use two printf statements to print the first and last indexes exactly as shown.
Practice
(1/5)
1. What does the term first occurrence of an element in an array mean?
easy
A. The last index where the element appears
B. The total count of the element in the array
C. The index of the first time the element appears in the array
D. The sum of all elements before the element
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 C
Quick Check:
First occurrence = first index found [OK]
Hint: First occurrence means earliest index found [OK]
Common Mistakes:
Confusing first occurrence with count of element
Mixing first occurrence with last occurrence
Thinking it means sum or total
2. Which of the following C code snippets correctly finds the first occurrence of x in array arr of size n?
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; i
Final Answer:
for(int i=0; i -> Option A
Quick Check:
Start to end loop with match check = for(int i=0; i
Hint: Loop from start, return index on first match [OK]
Common Mistakes:
Looping backward for first occurrence
Not handling element not found case
Returning wrong index when element differs
3. What will be the output of the following C code?
int arr[] = {2, 4, 6, 4, 8};
int n = 5, x = 4;
int first = -1, last = -1;
for(int i=0; i
medium
A. 1 3
B. 3 1
C. 0 4
D. -1 -1
Solution
Step 1: Trace the loop to find first and last occurrence
Hint: Track first when unset, update last on every match [OK]
Common Mistakes:
Swapping first and last values
Not updating last occurrence
Ignoring first occurrence initialization
4. Identify the error in the following code that tries to find the last occurrence of x in array arr of size n:
int lastOccurrence(int arr[], int n, int x) {
int last = -1;
for(int i=0; i
medium
A. Loop should run from n-1 to 0 for last occurrence
B. Using assignment (=) instead of comparison (==) in if condition
C. Variable last should be initialized to 0
D. Return statement should be inside the loop
Solution
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 B
Quick Check:
Use '==' for comparison, not '=' [OK]
Hint: Use '==' for comparison, '=' is assignment [OK]
Common Mistakes:
Using '=' instead of '==' in conditions
Loop direction confusion for last occurrence
Wrong initialization of variables
5. Given a sorted array arr of size n with possible duplicates, which approach is the most efficient to find the first and last occurrence of element x?
hard
A. Sort the array again and then find occurrences
B. Traverse the array from start to end once to find both occurrences
C. Use linear search twice: once from start, once from end
D. Use two binary searches: one for first occurrence, one for last occurrence
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 D