• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
September 23, 2023 |1.4K Views
PROBLEM OF THE DAY: 22/09/2023 | First and last occurrences of x
Description
Discussion

Welcome to the daily solving of our PROBLEM OF THE DAY with Karan Mashru. We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Binary Search but will also help you build up problem-solving skills.

In this video, we are given, a sorted array arr containing n elements with possibly duplicate is to find indexes of first elements. The task is to find the first and last occurrences of an element x in the given array.
Note: If the number x is not found in the array then return both the indices as -1.


Example :

Input:
n=9, x=5
arr[] = { 1, 3, 5, 5, 5, 5, 67, 123, 125 }
Output:  
2 5

Explanation: 
First occurrence of 5 is at index 2 and last occurrence of 5 is at index 5. 

Give the problem a try before going through the video. All the best!!!

Problem Link: https://practice.geeksforgeeks.org/problems/first-and-last-occurrences-of-x3116/1
Solution IDE Link: https://ide.geeksforgeeks.org/online-cpp-compiler/f14fead5-bcdb-471a-aeb6-4dab8e88ba07