Find First and Last Position of Element in Sorted Array.

Find First and Last Position of Element in Sorted Array.

Given an array of integers?nums?sorted in non-decreasing order, find the starting and ending position of a given?target?value.

If?target?is not found in the array, return?[-1, -1].

You must?write an algorithm with?O(log n)?runtime complexity.

?

Example 1:

Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]        

Example 2:

Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]        

Example 3:

Input: nums = [], target = 0
Output: [-1,-1]        

?

Constraints:

  • 0 <= nums.length <= 105
  • -109?<= nums[i]?<= 109
  • nums?is a non-decreasing array.
  • -109?<= target?<= 109


Solution:



? ? ? ?

? ? int startingPosition = -1, endingPosition = -1;

? ? ? ? int n = nums.size();

? ? ? ? for(int i=0; i<n; i++){

? ? ? ? ? ? if(nums[i] == target){

? ? ? ? ? ? ? ? startingPosition = i;

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? for(int i=n-1; i>=0; i--){

? ? ? ? ? ? if(nums[i] == target){

? ? ? ? ? ? ? ? endingPosition = i;

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return {startingPosition, endingPosition};

? ? }

要查看或添加评论,请登录

Abhishek Singh的更多文章

  • React JS Machine Coding: 25+ Must-Do Questions

    React JS Machine Coding: 25+ Must-Do Questions

    1. Weather App A weather application that fetches and displays real-time weather information based on the user’s…

  • Why One Should Choose Next.js For Frontend Framework ?

    Why One Should Choose Next.js For Frontend Framework ?

    Why Next.js? The world today is hyperconnected! It means if you run a business, then app development isn’t an option…

  • Mongoose

    Mongoose

    In MongoDB, "mongoose" is not a term for a specific action, but rather a popular JavaScript library used for…

  • Tail Recursion

    Tail Recursion

    Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by…

  • Visual Studio shortcut keys

    Visual Studio shortcut keys

    # General Shortcuts Ctrl-X or Shift-Delete: Cuts the currently selected item to the clipboard. Without selection it…

  • Pointers, References and Dynamic Memory Allocation.

    Pointers, References and Dynamic Memory Allocation.

    C++ Programming Language Pointers, References and Dynamic Memory Allocation Pointers, References and Dynamic Memory…

  • Search in a Rotated Array.

    Search in a Rotated Array.

    Given a sorted and rotated array A of N distinct elements which is rotated at some point, and given an element key. The…

  • Number of occurrence.

    Number of occurrence.

    class Solution{ public: /* if x is present in arr[] then returns the count of occurrences of x, otherwise returns 0. */…

  • Search insert position of K in a sorted array.

    Search insert position of K in a sorted array.

    Given a sorted array Arr[](0-index based) consisting of N distinct integers and an integer k, the task is to find the…

  • Ceil The Floor.

    Ceil The Floor.

    Given an unsorted array Arr[] of N integers and an integer X, find floor and ceiling of X in Arr[0..

社区洞察