Find Equal (or Middle) Point in a sorted array with duplicates
Given a sorted array of n size, the task is to find whether an element exists in the array from where the number of smaller elements is equal to the number of greater elements.
If Equal Point appears multiple times in input array, return the index of its first occurrence. If doesn’t exist, return -1.
Examples :
Input : arr[] = {1, 2, 3, 3, 3, 3}
Output : 1
Equal Point is arr[1] which is 2. Number of
elements smaller than 2 and greater than 2
are same.
Input : arr[] = {1, 2, 3, 3, 3, 3, 4, 4}
Output : Equal Point does not exist.
Input : arr[] = {1, 2, 3, 4, 4, 5, 6, 6, 6, 7}
Output : 3
First occurrence of equal point is arr[3]
A Naive approach is to take every element and count how many elements are smaller than that and then greater element. Then compare if both are equal or not.
An Efficient approach is to create an auxiliary array and store all distinct elements in it. If the count of distinct elements is even, then Equal Point does not exist. If count is odd, then the equal point is the middle point of the auxiliary array.
Below is implementation of above idea.
C++
#include <bits/stdc++.h>
using namespace std;
int findEqualPoint( int arr[], int n)
{
int distArr[n];
int i = 0, di = 0;
while (i < n)
{
distArr[di++] = i++;
while (i<n && arr[i] == arr[i-1])
i++;
}
return (di & 1)? distArr[di>>1] : -1;
}
int main()
{
int arr[] = {1, 2, 3, 4, 4, 5, 6, 6, 6, 7};
int n = sizeof (arr)/ sizeof (arr[0]);
int index = findEqualPoint(arr, n);
if (index != -1)
cout << "Equal Point = " << arr[index] ;
else
cout << "Equal Point does not exists" ;
return 0;
}
|
Java
class Test
{
static int findEqualPoint( int arr[], int n)
{
int distArr[] = new int [n];
int i = 0 , di = 0 ;
while (i < n)
{
distArr[di++] = i++;
while (i<n && arr[i] == arr[i- 1 ])
i++;
}
return (di & 1 )!= 0 ? distArr[di>> 1 ] : - 1 ;
}
public static void main(String args[])
{
int arr[] = { 1 , 2 , 3 , 4 , 4 , 5 , 6 , 6 , 6 , 7 };
int index = findEqualPoint(arr, arr.length);
System.out.println(index != - 1 ? "Equal Point = " + arr[index]
: "Equal Point does not exists" );
}
}
|
Python 3
def findEqualPoint(arr, n):
distArr = [ 0 ] * n
i = 0
di = 0
while (i < n):
distArr[di] = i
di + = 1
i + = 1
while (i < n and
arr[i] = = arr[i - 1 ]):
i + = 1
return distArr[di >> 1 ] if (di & 1 ) else - 1
arr = [ 1 , 2 , 3 , 4 , 4 ,
5 , 6 , 6 , 6 , 7 ]
n = len (arr)
index = findEqualPoint(arr, n)
if (index ! = - 1 ):
print ( "Equal Point = " ,
arr[index])
else :
print ( "Equal Point does " +
"not exists" )
|
C#
using System;
class GFG
{
static int findEqualPoint( int []arr,
int n)
{
int []distArr = new int [n];
int i = 0, di = 0;
while (i < n)
{
distArr[di++] = i++;
while (i < n && arr[i] == arr[i - 1])
i++;
}
return (di & 1) != 0 ?
distArr[di >> 1] :
-1;
}
public static void Main()
{
int []arr = {1, 2, 3, 4, 4,
5, 6, 6, 6, 7};
int index = findEqualPoint(arr, arr.Length);
Console.Write(index != -1 ?
"Equal Point = " + arr[index] :
"Equal Point does not exists" );
}
}
|
PHP
<?php
function findEqualPoint( $arr , $n )
{
$distArr = array ();
$i = 0; $di = 0;
while ( $i < $n )
{
$distArr [ $di ++] = $i ++;
while ( $i < $n and
$arr [ $i ] == $arr [ $i -1])
$i ++;
}
return ( $di & 1)? $distArr [ $di >>1] : -1;
}
$arr = array (1, 2, 3, 4, 4, 5, 6, 6, 6, 7);
$n = count ( $arr );
$index = findEqualPoint( $arr , $n );
if ( $index != -1)
echo "Equal Point = " , $arr [ $index ] ;
else
echo "Equal Point does not exists" ;
?>
|
Javascript
<script>
function findEqualPoint(arr, n)
{
let distArr = new Array(n);
distArr.fill(0);
let i = 0, di = 0;
while (i < n)
{
distArr[di++] = i++;
while (i < n && arr[i] == arr[i - 1])
i++;
}
return (di & 1) != 0 ?
distArr[di >> 1] :
-1;
}
let arr = [1, 2, 3, 4, 4, 5, 6, 6, 6, 7];
let index = findEqualPoint(arr, arr.length);
document.write(index != -1 ?
"Equal Point = " + arr[index] :
"Equal Point does not exists" );
</script>
|
Time Complexity : O(n)
Auxiliary Space : O(n)
Space Optimization :
We can reduce extra space by traversing the array twice instead of once.
- Count total distinct elements by doing a traversal of input array. Let this count be distCount.
- If distCount is even, return -1.
- If distCount is odd, traverse the array again and stop at distCount/2 and return this index.
Thanks to Pavan Kumar J S for suggesting this space-optimized approach.
Last Updated :
11 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...