Find Last Occurrence

Easy Arrays
Solve in Playground →

Problem Statement

Write a C program to find the index of the last occurrence of a given element in an array, or -1 if not present.

Input Format

First line contains n. Second line contains n space-separated integers. Third line contains the target element.

Output Format

Print the index of the last occurrence or -1.

Constraints

1 <= n <= 100

Sample Input

6
1 2 3 2 4 2
2

Sample Output

5

Explanation

Iterate backwards from the end of the array to find the last matching element and return its index.

Starter Code

#include <stdio.h>

int main() {
    int n;
    if (scanf("%d", &n) == 1) {
        int arr[100];
        for (int i = 0; i < n; i++) {
            scanf("%d", &arr[i]);
        }
        int target;
        scanf("%d", &target);
        // Write your code here to find last occurrence
    }
    return 0;
}

Limits

  • Time Limit: 1s
  • Memory Limit: 256MB

Embedded C Programming

Updated: March 15, 2026
Intermediate

Embedded systems rely on efficient low-level programming to interact directly with hardware. In this course, you will learn how to write practical Embedded C programs used in real microcontroller-based systems. Rather than focusing only on theory, this course follows a practice-driven approach. Each lesson includes hands-on coding exercises that simulate real firmware development tasks used