Find index of first occurrence when an unsorted array is sorted
Last Updated :
29 Aug, 2022
Given an unsorted array and a number x, find an index of first occurrence of x when we sort the array. If x is not present, print -1.
Examples:
Input : arr[] = {10, 30, 20, 50, 20}
x = 20
Output : 1
Sorted array is {10, 20, 20, 30, 50}
Input : arr[] = {10, 30, 20, 50, 20}
x = 60
Output : -1
60 is not present in array.
A simple solution is to first sort the array, then do binary search to find first occurrence.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int findFirst( int arr[], int n, int x)
{
sort(arr, arr + n);
int * ptr = lower_bound(arr, arr + n, x);
return (*ptr != x) ? -1 : (ptr - arr);
}
int main()
{
int x = 20, arr[] = { 10, 30, 20, 50, 20 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << findFirst(arr, n, x);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int findFirst( int arr[], int n, int x)
{
Arrays.sort(arr);
int ptr = lowerBound(arr, 0 , n, x);
return (arr[ptr] != x) ? - 1 : (ptr);
}
static int lowerBound( int [] a, int low, int high,
int element)
{
while (low < high) {
int middle = low + (high - low) / 2 ;
if (element > a[middle])
low = middle + 1 ;
else
high = middle;
}
return low;
}
public static void main(String[] args)
{
int x = 20 , arr[] = { 10 , 30 , 20 , 50 , 20 };
int n = arr.length;
System.out.println(findFirst(arr, n, x));
}
}
|
Python3
import math
def findFirst(arr, n, x):
arr.sort()
ptr = lowerBound(arr, 0 , n, x)
return 1 if (ptr ! = x) else (ptr - arr)
def lowerBound(a, low, high, element):
while (low < high):
middle = low + (high - low) / / 2
if (element > a[middle]):
low = middle + 1
else :
high = middle
return low
if __name__ = = '__main__' :
x = 20
arr = [ 10 , 30 , 20 , 50 , 20 ]
n = len (arr)
print (findFirst(arr, n, x))
|
C#
using System;
class GFG {
static int findFirst( int [] arr, int n, int x)
{
Array.Sort(arr);
int ptr = lowerBound(arr, 0, n, x);
return (arr[ptr] != x) ? -1 : (ptr);
}
static int lowerBound( int [] a, int low, int high,
int element)
{
while (low < high) {
int middle = low + (high - low) / 2;
if (element > a[middle])
low = middle + 1;
else
high = middle;
}
return low;
}
static public void Main()
{
int x = 20;
int [] arr = { 10, 30, 20, 50, 20 };
int n = arr.Length;
Console.Write(findFirst(arr, n, x));
}
}
|
PHP
<?php
function findFirst( $arr , $n , $x )
{
sort( $arr );
$ptr = floor ( $arr );
return ( $ptr != $x )? 1 : ( $ptr - $arr );
}
$x = 20;
$arr = array (10, 30, 20, 50, 20);
$n = sizeof( $arr )/sizeof( $arr [0]);
echo findFirst( $arr , $n , $x );
#This code is contributed by Tushil.
?>
|
Javascript
<script>
function findFirst(arr, n, x)
{
arr.sort();
let ptr = lowerBound(arr, 0, n, x);
return (arr[ptr] != x) ? -1 : (ptr);
}
function lowerBound(a, low, high, element)
{
while (low < high)
{
let middle = low + parseInt(
(high - low) / 2, 10);
if (element > a[middle])
low = middle + 1;
else
high = middle;
}
return low;
}
let x = 20;
let arr = [ 10, 30, 20, 50, 20 ];
let n = arr.length;
document.write(findFirst(arr, n, x));
</script>
|
Complexity Analysis:
- Time Complexity : O(n Log n)
- Auxiliary Space: O(1)
An efficient solution is to simply count smaller elements than x.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int findFirst( int arr[], int n, int x)
{
int count = 0;
bool isX = false ;
for ( int i = 0; i < n; i++) {
if (arr[i] == x)
isX = true ;
else if (arr[i] < x)
count++;
}
return (isX == false ) ? -1 : count;
}
int main()
{
int x = 20, arr[] = { 10, 30, 20, 50, 20 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << findFirst(arr, n, x);
return 0;
}
|
Java
public class GFG {
static int findFirst( int arr[], int n, int x)
{
int count = 0 ;
boolean isX = false ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] == x) {
isX = true ;
}
else if (arr[i] < x) {
count++;
}
}
return (isX == false ) ? - 1 : count;
}
public static void main(String[] args)
{
int x = 20 , arr[] = { 10 , 30 , 20 , 50 , 20 };
int n = arr.length;
System.out.println(findFirst(arr, n, x));
}
}
|
Python3
def findFirst(arr, n, x):
count = 0
isX = False
for i in range (n):
if (arr[i] = = x):
isX = True
elif (arr[i] < x):
count + = 1
return - 1 if (isX = = False ) else count
if __name__ = = "__main__" :
x = 20
arr = [ 10 , 30 , 20 , 50 , 20 ]
n = len (arr)
print (findFirst(arr, n, x))
|
C#
using System;
public class GFG {
static int findFirst( int [] arr, int n, int x)
{
int count = 0;
bool isX = false ;
for ( int i = 0; i < n; i++) {
if (arr[i] == x) {
isX = true ;
}
else if (arr[i] < x) {
count++;
}
}
return (isX == false ) ? -1 : count;
}
public static void Main()
{
int x = 20;
int [] arr = { 10, 30, 20, 50, 20 };
int n = arr.Length;
Console.WriteLine(findFirst(arr, n, x));
}
}
|
PHP
<?php
function findFirst( $arr , $n , $x )
{
$count = 0;
$isX = false;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $arr [ $i ] == $x )
$isX = true;
else if ( $arr [ $i ] < $x )
$count ++;
}
return ( $isX == false)? -1 : $count ;
}
$x = 20;
$arr = array (10, 30, 20, 50, 20);
$n = sizeof( $arr );
echo findFirst( $arr , $n , $x );
?>
|
Javascript
<script>
function findFirst(arr, n, x)
{
var count = 0;
var isX = false ;
for ( var i = 0; i < n; i++)
{
if (arr[i] == x)
{
isX = true ;
}
else if (arr[i] < x)
{
count++;
}
}
return (isX == false ) ? -1 : count;
}
var x = 20, arr = [ 10, 30, 20, 50, 20 ];
var n = arr.length;
document.write(findFirst(arr, n, x));
</script>
|
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...