Find First and Last Occurrence of a Given Number

Learning 1D Array Binary Search DSA practice problem on Onlearn.

Difficulty: easy.

Topics: Finding the index of the last occurrence of a target in a sorted array, Arrays, Binary Search, Linear Search, Time Complexity, Space Complexity, complexity analysis, optimization, array, search algorithms, binary search, array indexing, Array Traversal & Indexing.

Problem Statement Given a sorted array of N integers, find the index of the last occurrence of a given target key. If the target is not found, return 1. Note: Consider 0 based indexing. Input Specification The first line contains an integer N, representing the size of the array. The second line contains an integer target, representing the key to be found. The third line contains N space separated integers, representing the elements of the sorted array. Output Specification Return an integer representing the 0 based index of the last occurrence of the target key. If the target is not found, return 1. Constraints No explicit constraints are provided in the original content beyond the array being sorted and using 0 based indexing. Sample Test Cases Example 1: Example 2: