-
Notifications
You must be signed in to change notification settings - Fork 0
/
question_13.cpp
58 lines (45 loc) · 1.36 KB
/
question_13.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
==> Find Index <==
Question Link:- https://practice.geeksforgeeks.org/problems/find-index4752/1?page=1&difficulty[]=-2&category[]=Arrays&sortBy=submissions
Given an unsorted array Arr[] of N integers and a Key which is present
in this array. You need to write a program to find the start
index( index where the element is first found from left in the array )
and end index( index where the element is first found from right in the array ).
Example 1:
Input:
N = 6
arr[] = { 1, 2, 3, 4, 5, 5 }
Key = 5
Output: 4 5
Explanation:
5 appears first time at index 4 and
appears last time at index 5.
(0 based indexing)
Example 2:
Input:
N=6
arr[] = { 6, 5, 4, 3, 1, 2 }
Key = 4
Output: 2 2
*/
#include <iostream>
#include <vector>
using namespace std;
void printVector( vector<int> v ) {
for ( int i = 0; i < v.size(); i++ ) cout << v[i] << " ";
}
vector<int> findIndex(int arr[], int n, int key) {
int x = 0, y = n-1;
for ( int i = 0; i < n; i++ ) {
if ( arr[x] != key ) x++;
if ( arr[y] != key ) y--;
}
if ( x == n ) x = -1;
return { x, y };
}
int main() {
int arr [] = { 21, 11, 30, 17, 30, 18, 13, 23, 10, 30, 11, 15, 21, 13, 13, 24, 25, 11, 15, 30, 15, 18, 13 };
vector<int> ans = findIndex(arr, 23, 899);
printVector(ans);
return 0;
}