From edbb1c3d5a507e03e60889802a4e749203037927 Mon Sep 17 00:00:00 2001 From: Akansha_chaurasia <125260131+Akansha77@users.noreply.github.com> Date: Sat, 27 Jan 2024 07:51:26 +0000 Subject: [PATCH] Day-26 q3: Find first and last position of element in sorted array #560 cpp code added by Akansha77 --- .../Akansha--C.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 Day-26/q3: Find first and last position of element in sorted array/Akansha--C.cpp diff --git a/Day-26/q3: Find first and last position of element in sorted array/Akansha--C.cpp b/Day-26/q3: Find first and last position of element in sorted array/Akansha--C.cpp new file mode 100644 index 00000000..fa1cde30 --- /dev/null +++ b/Day-26/q3: Find first and last position of element in sorted array/Akansha--C.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + vector searchRange(vector& nums, int target) { + if(binary_search(nums.begin(),nums.end(),target)) + { + int idx=lower_bound(nums.begin(),nums.end(),target)-nums.begin(); + int idx1=upper_bound(nums.begin(),nums.end(),target)-nums.begin(); + return {idx,idx1-1}; + } + return {-1,-1}; + } +}; \ No newline at end of file