`{"id":"28d65a0b-7691-440a-83fc-0e9076ea9c54","name":"Find First And Last Postion Of Element In Sorted Array","description":"1. Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.\r\n2. If target is not found in the array, return [-1, -1].\r\n3. You must write an algorithm with O(log n) runtime complexity.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"0 &lt;= nums.length &lt;= 10^5\r\n-10^9 &lt;= nums[i] &lt;= 10^9\r\nnums is a non-decreasing array.\r\n-10^9 &lt;= target &lt;= 10^9","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvoid solution(vector<int>& arr, int target, vector<int>& ans){\n //Write your code here \n}\n\nint main(){\n int n;\n cin>>n;\n vector<int> arr(n,0);\n for(int i=0; i<n; i++){\n cin>>arr[i];\n }\n int tar;\n cin>>tar;\n vector<int> ans(2, -1);\n solution(arr, tar, ans);\n cout<<ans<<\" \"<<ans;\n \n \n \n \n}"},"java":{"code":"import java.util.*;\r\nimport java.io.*;\r\n\r\npublic class Main {\r\n\r\n //return an array of size two, which contains first and last index of target.\r\n public static int[] find(int[]arr,int target) {\r\n //write your code here\r\n return null;\r\n }\r\n\r\n public static void main(String[]args) {\r\n //input work\r\n Scanner scn = new Scanner(System.in);\r\n\r\n int n = scn.nextInt();\r\n int[]arr = new int[n];\r\n\r\n for(int i=0;i < n;i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n\r\n int target = scn.nextInt();\r\n\r\n int[]ans = find(arr,target);\r\n System.out.println(ans + \" \" + ans);\r\n }\r\n}"},"python":{"code":"def main():\r\n pass\r\n\r\nif _name_ == '__main__':\r\n main()"}},"points":10,"difficulty":"easy","sampleInput":"7\r\n2 2 3 3 3 4 5\r\n3","sampleOutput":"2 4","questionVideo":"https://www.youtube.com/embed/Y7LiLNdayF0","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"cb36811c-9cd7-4d80-aa52-ae9b8409862a","name":"Searching And Sorting For Intermediate","slug":"searching-and-sorting-for-intermediate-10001","type":0},{"id":"0e866bdc-aaeb-4c9f-85fd-d7d79645af90","name":"Find First And Last Postion Of Element In Sorted Array","slug":"find-first-and-last-postion-of-element-in-sorted-array","type":1}],"next":{"id":"aacde5dc-f504-42ba-bb24-41c184fbd800","name":"Find First And Last Postion Of Element In Sorted Array MCQ","type":0,"slug":"find-first-and-last-postion-of-element-in-sorted-array-mcq"},"prev":{"id":"e0a55161-d8b5-423f-a6f3-f96a2c59fdfc","name":"Find first and last position of element in sorted array","type":3,"slug":"find-first-and-last-position-of-element-in-sorted-array"}}}`

# Find First And Last Postion Of Element In Sorted Array

1. Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value. 2. If target is not found in the array, return [-1, -1]. 3. You must write an algorithm with O(log n) runtime complexity.

`{"id":"28d65a0b-7691-440a-83fc-0e9076ea9c54","name":"Find First And Last Postion Of Element In Sorted Array","description":"1. Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.\r\n2. If target is not found in the array, return [-1, -1].\r\n3. You must write an algorithm with O(log n) runtime complexity.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"0 &lt;= nums.length &lt;= 10^5\r\n-10^9 &lt;= nums[i] &lt;= 10^9\r\nnums is a non-decreasing array.\r\n-10^9 &lt;= target &lt;= 10^9","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvoid solution(vector<int>& arr, int target, vector<int>& ans){\n //Write your code here \n}\n\nint main(){\n int n;\n cin>>n;\n vector<int> arr(n,0);\n for(int i=0; i<n; i++){\n cin>>arr[i];\n }\n int tar;\n cin>>tar;\n vector<int> ans(2, -1);\n solution(arr, tar, ans);\n cout<<ans<<\" \"<<ans;\n \n \n \n \n}"},"java":{"code":"import java.util.*;\r\nimport java.io.*;\r\n\r\npublic class Main {\r\n\r\n //return an array of size two, which contains first and last index of target.\r\n public static int[] find(int[]arr,int target) {\r\n //write your code here\r\n return null;\r\n }\r\n\r\n public static void main(String[]args) {\r\n //input work\r\n Scanner scn = new Scanner(System.in);\r\n\r\n int n = scn.nextInt();\r\n int[]arr = new int[n];\r\n\r\n for(int i=0;i < n;i++) {\r\n arr[i] = scn.nextInt();\r\n }\r\n\r\n int target = scn.nextInt();\r\n\r\n int[]ans = find(arr,target);\r\n System.out.println(ans + \" \" + ans);\r\n }\r\n}"},"python":{"code":"def main():\r\n pass\r\n\r\nif _name_ == '__main__':\r\n main()"}},"points":10,"difficulty":"easy","sampleInput":"7\r\n2 2 3 3 3 4 5\r\n3","sampleOutput":"2 4","questionVideo":"https://www.youtube.com/embed/Y7LiLNdayF0","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"cb36811c-9cd7-4d80-aa52-ae9b8409862a","name":"Searching And Sorting For Intermediate","slug":"searching-and-sorting-for-intermediate-10001","type":0},{"id":"0e866bdc-aaeb-4c9f-85fd-d7d79645af90","name":"Find First And Last Postion Of Element In Sorted Array","slug":"find-first-and-last-postion-of-element-in-sorted-array","type":1}],"next":{"id":"aacde5dc-f504-42ba-bb24-41c184fbd800","name":"Find First And Last Postion Of Element In Sorted Array MCQ","type":0,"slug":"find-first-and-last-postion-of-element-in-sorted-array-mcq"},"prev":{"id":"e0a55161-d8b5-423f-a6f3-f96a2c59fdfc","name":"Find first and last position of element in sorted array","type":3,"slug":"find-first-and-last-position-of-element-in-sorted-array"}}}` Editor

# Find First And Last Postion Of Element In Sorted Array

easy

1. Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value. 2. If target is not found in the array, return [-1, -1]. 3. You must write an algorithm with O(log n) runtime complexity.

## Constraints

0 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9 nums is a non-decreasing array. -10^9 <= target <= 10^9

## Format

### Input

Input is managed for you

### Output

Output is managed for you

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}7 2 2 3 3 3 4 5 3```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}2 4`

Question Video

Discussions

Show Discussion

Related Resources 