Given an array of integers, task is to find the starting and ending position of a given key.
Examples:
Input : arr[] = {1, 2, 3, 4, 5, 5}
Key = 5
Output : Start index: 4
Last index: 5
Explanation: Starting index where 5
is present is 4 and ending index is 5.
Input :arr[] = {1, 3, 7, 8, 6},
Key = 2
Output : Key not present in array
Input :arr[] = {1, 8, 7, 8, 6},
Key = 7
Output : Only one occurrence of
key is present at index 2
We traverse array from beginning to find first occurrence. If element is present, then we traverse from end also to find last occurrence.
Implementation:
C++
#include <iostream>
using namespace std;
void findIndex( int a[], int n, int key)
{
int start = -1;
for ( int i = 0; i < n; i++) {
if (a[i] == key) {
start = i;
break ;
}
}
if (start == -1) {
cout << "Key not present in array" ;
return ;
}
int end = start;
for ( int i = n - 1; i >= start; i--) {
if (a[i] == key) {
end = i;
break ;
}
}
if (start == end)
cout << "Only one key is present at index : "
<< start;
else {
cout << "Start index: " << start;
cout << "\n" ;
cout << "Last index: " << end;
}
}
int main()
{
int a[] = { 1, 2, 7, 8, 8, 9, 8, 0, 0, 0, 8 };
int n = sizeof (a) / sizeof (a[0]);
int key = 8;
findIndex(a, n, key);
return 0;
}
|
Java
class Test {
static void findIndex( int a[], int n, int key)
{
int start = - 1 ;
for ( int i = 0 ; i < n; i++) {
if (a[i] == key) {
start = i;
break ;
}
}
if (start == - 1 ) {
System.out.println( "Key not present in array" );
return ;
}
int end = start;
for ( int i = n - 1 ; i >= start; i--) {
if (a[i] == key) {
end = i;
break ;
}
}
if (start == end)
System.out.println( "Only one key is present at index : " + start);
else {
System.out.println( "Start index: " + start);
System.out.println( "Last index: " + end);
}
}
public static void main(String args[])
{
int a[] = { 1 , 2 , 7 , 8 , 8 , 9 , 8 , 0 , 0 , 0 , 8 };
int key = 8 ;
findIndex(a, a.length, key);
}
}
|
Python3
def findIndex (a, n, key ):
start = - 1
for i in range (n):
if a[i] = = key:
start = i
break
if start = = - 1 :
print ( "Key not present in array" )
return 0
end = start
for i in range (n - 1 , start - 1 , - 1 ):
if a[i] = = key:
end = i
break
if start = = end:
print ( "Only one key is present at index : " , start)
else :
print ( "Start index: " , start)
print ( "Last index: " , end)
a = [ 1 , 2 , 7 , 8 , 8 , 9 , 8 , 0 , 0 , 0 , 8 ]
n = len (a)
key = 8
findIndex(a, n, key)
|
C#
using System;
class GFG {
static void findIndex( int [] a, int n,
int key)
{
int start = -1;
for ( int i = 0; i < n; i++) {
if (a[i] == key) {
start = i;
break ;
}
}
if (start == -1) {
Console.WriteLine( "Key not "
+ "present in array" );
return ;
}
int end = start;
for ( int i = n - 1; i >= start; i--) {
if (a[i] == key) {
end = i;
break ;
}
}
if (start == end)
Console.WriteLine( "Only one key is"
+ " present at index : "
+ start);
else {
Console.WriteLine( "Start index: "
+ start);
Console.WriteLine( "Last index: "
+ end);
}
}
public static void Main()
{
int [] a = { 1, 2, 7, 8, 8, 9,
8, 0, 0, 0, 8 };
int key = 8;
findIndex(a, a.Length, key);
}
}
|
PHP
<?php
function findIndex( $arr , $find ){
$start = -1;
$end = -1;
foreach ( $arr as $key => $value ) {
if ( $find === $value ){
if ( $start ==-1)
$start = $key ;
$end = $key ;
}
}
if ( $start ==-1){
return "Key not present in array" ;
}
if ( $end == $start ){
return "Only one key is present " . " at index : " . $start ;
}
return "Start index: " . $start . "\nLast index: " . $end ;
}
$a = array (1, 2, 7, 8, 8, 9, 8, 0, 0, 0, 8);
$key = 8;
echo findIndex( $a , $key );
?>
|
Javascript
<script>
function findIndex(a, n, key)
{
let start = -1;
for (let i = 0; i < n; i++) {
if (a[i] == key) {
start = i;
break ;
}
}
if (start == -1) {
document.write( "Key not present in array" );
return ;
}
let end = start;
for (let i = n - 1; i >= start; i--) {
if (a[i] == key) {
end = i;
break ;
}
}
if (start == end)
document.write( "Only one key is present at index : "
+ start);
else {
document.write( "Start index: " + start);
document.write( "<br>" + "Last index: " + end);
}
}
let a = [ 1, 2, 7, 8, 8, 9, 8, 0, 0, 0, 8 ];
let n = a.length;
let key = 8;
findIndex(a, n, key);
</script>
|
OutputStart index: 3
Last index: 10
Time Complexity: Worst case time complexity is O(N), ( when we traverse the whole array and don’t find the element’s start and last indices), where N represents the size of the given array. and best case time complexity will be O(1), when start index is ‘0’ and last index is ‘n – 1’.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Related Article:
Find first and last occurrences of an element in a sorted array
This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.