[Solved] First and Last Position of an Element In Sorted Array

First and Last Position of an Element In Sorted Array: You have been given a sorted array/list ARR consisting of ‘N’ elements. You are also given an integer ‘K’.

Now, your task is to find the first and last occurrence of ‘K’ in ARR.

First and Last Position of an Element In Sorted Array

Problem: https://www.codingninjas.com/codestudio/problems/first-and-last-position-of-an-element-in-sorted-array_1082549

Note :

1. If ‘K’ is not present in the array, then the first and the last occurrence will be -1. 
2. ARR may contain duplicate elements.

For example, if ARR = [0, 1, 1, 5] and K = 1, then the first and last occurrence of 1 will be 1(0 – indexed) and 2.

Input Format

The first line of input contains an integer 'T' which denotes the number of test cases or queries to be run. Then the test cases follow.

The first line of each test case contains two single-space separated integers ‘N’ and ‘K’, respectively.

The second line of each test case contains ‘N’ single space-separated integers denoting the elements of the array/list ARR.

Output Format :

Return two single-space separated integers denoting the first and the last occurrence of ‘K’ in ARR, respectively.

Note:

You do not need to print anything; it has already been taken care of. Just implement the given function.

Constraints:

1 <= T <= 100
1 <= N <= 5000
0 <= K <= 10^5
0 <= ARR[i] <=10^5

Time Limit : 1 second

Sample Input 1:

2
6 3
0 5 5 6 6 6
8 2
0 0 1 1 2 2 2 2

Sample Output 1:

-1 -1 
4 7

Explanation Of Sample Output 1:

For the first test case, 3 is not present in the array. Hence the first and last occurrence of 3 is -1 and -1.

For the second test case, the first occurrence of 2 in at index 4 and last occurrence is at index 7.

Sample Input 2:

2
4 0
0 0 0 0
1 2
2

Sample Output 2:

0 3
0 0

Solution:

import java.util.ArrayList;
import javafx.util.Pair;
public class Solution {
    
    static int first(ArrayList<Integer> arr, int n, int k){
        int beg = 0;
        int end = n - 1;
        int output = -1;
        
        int mid = beg - (beg - end)/2;
        
        while(beg<=end){
            
            if(arr.get(mid) == k){
                output = mid;
                end = mid - 1;
            }
            else if(arr.get(mid) < k){
                beg = mid + 1;
            }
            else{
                end = mid - 1;
            }
            
            mid = beg - (beg - end)/2;
        }
        
        return output;
    }
    
    static int last(ArrayList<Integer> arr, int n, int k){
        int beg = 0;
        int end = n - 1;
        int output = -1;
        
        int mid = beg - (beg - end)/2;
        
        while(beg<=end){
            
            if(arr.get(mid) == k){
                output = mid;
                beg = mid + 1;
            }
            else if(arr.get(mid) < k){
                beg = mid + 1;
            }
            else{
                end = mid - 1;
            }
            
            mid = beg - (beg - end)/2;
        }
        
        return output;
    }

    public static int[] firstAndLastPosition(ArrayList<Integer> arr, int n, int k) {
        // Write your code here.
        int[] temp = {first(arr,n,k),last(arr,n,k)};
        
        return temp;
    }

};
int first(vector<int>& arr, int n, int k){
        int beg = 0;
        int end = n - 1;
        int output = -1;
        
        int mid = beg - (beg - end)/2;
        
        while(beg<=end){
            
            if(arr[mid] == k){
                output = mid;
                end = mid - 1;
            }
            else if(arr[mid] < k){
                beg = mid + 1;
            }
            else{
                end = mid - 1;
            }
            
            mid = beg - (beg - end)/2;
        }
        
        return output;
    }

int last(vector<int>& arr, int n, int k){
        int beg = 0;
        int end = n - 1;
        int output = -1;
        
        int mid = beg - (beg - end)/2;
        
        while(beg<=end){
            
            if(arr[mid] == k){
                output = mid;
                beg = mid + 1;
            }
            else if(arr[mid] < k){
                beg = mid + 1;
            }
            else{
                end = mid - 1;
            }
            
            mid = beg - (beg - end)/2;
        }
        
        return output;
    }

pair<int, int> firstAndLastPosition(vector<int>& arr, int n, int k)
{
    // Write your code here
    pair<int,int> temp;
    temp.first = first(arr,n,k);
    temp.second = last(arr,n,k);
    
    return temp;
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *