Find Equal (or Middle) Point in a sorted array with duplicates
Last Updated :
11 Sep, 2023
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++
// C++ program to find Equal point in a sorted array
// which may have many duplicates.
#include <bits/stdc++.h>
using namespace std;
// Returns value of Equal point in a sorted array arr[]
// It returns -1 if there is no Equal Point.
int findEqualPoint(int arr[], int n)
{
// To store first indexes of distinct elements of arr[]
int distArr[n];
// Traverse input array and store indexes of first
// occurrences of distinct elements in distArr[]
int i = 0, di = 0;
while (i < n)
{
// This element must be first occurrence of a
// number (this is made sure by below loop),
// so add it to distinct array.
distArr[di++] = i++;
// Avoid all copies of arr[i] and move to next
// distinct element.
while (i<n && arr[i] == arr[i-1])
i++;
}
// di now has total number of distinct elements.
// If di is odd, then equal point exists and is at
// di/2, otherwise return -1.
return (di & 1)? distArr[di>>1] : -1;
}
// Driver code
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
//Java program to find Equal point in a sorted array
// which may have many duplicates.
class Test
{
// Returns value of Equal point in a sorted array arr[]
// It returns -1 if there is no Equal Point.
static int findEqualPoint(int arr[], int n)
{
// To store first indexes of distinct elements of arr[]
int distArr[] = new int[n];
// Traverse input array and store indexes of first
// occurrences of distinct elements in distArr[]
int i = 0, di = 0;
while (i < n)
{
// This element must be first occurrence of a
// number (this is made sure by below loop),
// so add it to distinct array.
distArr[di++] = i++;
// Avoid all copies of arr[i] and move to next
// distinct element.
while (i<n && arr[i] == arr[i-1])
i++;
}
// di now has total number of distinct elements.
// If di is odd, then equal point exists and is at
// di/2, otherwise return -1.
return (di & 1)!=0 ? distArr[di>>1] : -1;
}
// Driver method
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
# Python 3 program to find
# Equal point in a sorted
# array which may have
# many duplicates.
# Returns value of Equal
# point in a sorted array
# arr[]. It returns -1 if
# there is no Equal Point.
def findEqualPoint(arr, n):
# To store first indexes of
# distinct elements of arr[]
distArr = [0] * n
# Traverse input array and
# store indexes of first
# occurrences of distinct
# elements in distArr[]
i = 0
di = 0
while (i < n):
# This element must be
# first occurrence of a
# number (this is made
# sure by below loop),
# so add it to distinct array.
distArr[di] = i
di += 1
i += 1
# Avoid all copies of
# arr[i] and move to
# next distinct element.
while (i < n and
arr[i] == arr[i - 1]):
i += 1
# di now has total number
# of distinct elements.
# If di is odd, then equal
# point exists and is at
# di/2, otherwise return -1.
return distArr[di >> 1] if (di & 1) else -1
# Driver code
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")
# This code is contributed
# by Smitha
C#
// C# program to find Equal
// point in a sorted array
// which may have many duplicates.
using System;
class GFG
{
// Returns value of Equal point
// in a sorted array arr[]
// It returns -1 if there
// is no Equal Point.
static int findEqualPoint(int []arr,
int n)
{
// To store first indexes of
// distinct elements of arr[]
int []distArr = new int[n];
// Traverse input array and
// store indexes of first
// occurrences of distinct
// elements in distArr[]
int i = 0, di = 0;
while (i < n)
{
// This element must be
// first occurrence of a
// number (this is made
// sure by below loop),
// so add it to distinct array.
distArr[di++] = i++;
// Avoid all copies of
// arr[i] and move to
// next distinct element.
while (i < n && arr[i] == arr[i - 1])
i++;
}
// di now has total number
// of distinct elements.
// If di is odd, then equal
// point exists and is at
// di/2, otherwise return -1.
return (di & 1) != 0 ?
distArr[di >> 1] :
-1;
}
// Driver Code
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
// PHP program to find Equal point in a
// sorted array which may have many
// duplicates.
// Returns value of Equal point in a
// sorted array arr[] It returns -1
// if there is no Equal Point.
function findEqualPoint( $arr, $n)
{
// To store first indexes of distinct
// elements of arr[]
$distArr = array();
// Traverse input array and store
// indexes of first occurrences of
// distinct elements in distArr[]
$i = 0; $di = 0;
while ($i < $n)
{
// This element must be first
// occurrence of a number (this
// is made sure by below loop),
// so add it to distinct array.
$distArr[$di++] = $i++;
// Avoid all copies of arr[i]
// and move to next distinct
// element.
while ($i < $n and
$arr[$i] == $arr[$i-1])
$i++;
}
// di now has total number of
// distinct elements. If di is odd,
// then equal point exists and is
// at di/2, otherwise return -1.
return ($di & 1)? $distArr[$di>>1] : -1;
}
// Driver code
$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";
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// JavaScript program to find Equal
// point in a sorted array
// which may have many duplicates.
// Returns value of Equal point
// in a sorted array arr[]
// It returns -1 if there
// is no Equal Point.
function findEqualPoint(arr, n)
{
// To store first indexes of
// distinct elements of arr[]
let distArr = new Array(n);
distArr.fill(0);
// Traverse input array and
// store indexes of first
// occurrences of distinct
// elements in distArr[]
let i = 0, di = 0;
while (i < n)
{
// This element must be
// first occurrence of a
// number (this is made
// sure by below loop),
// so add it to distinct array.
distArr[di++] = i++;
// Avoid all copies of
// arr[i] and move to
// next distinct element.
while (i < n && arr[i] == arr[i - 1])
i++;
}
// di now has total number
// of distinct elements.
// If di is odd, then equal
// point exists and is at
// di/2, otherwise return -1.
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.
Similar Reads
Last duplicate element in a sorted array
We have a sorted array with duplicate elements and we have to find the index of last duplicate element and print index of it and also print the duplicate element. If no such element found print a message. Examples: Input : arr[] = {1, 5, 5, 6, 6, 7} Output : Last index: 4 Last duplicate item: 6 Inpu
6 min read
Find a Fixed Point in an array with duplicates allowed
Given an array of n duplicates or distinct integers sorted in ascending order, write a function that returns a Fixed Point in the array, if there is any Fixed Point present in the array, else returns -1. Fixed Point in an array is an index i such that arr[i] is equal to i. Note that integers in the
8 min read
Find a Fixed Point (Value equal to index) in a given array | Duplicates Allowed
Given an array of n integers sorted in ascending order, write a function that returns a Fixed Point in the array, if there is any Fixed Point present in the array, else returns -1. Fixed Point in an array is an index i such that arr[i] is equal to i. Note that integers in the array can be negative.
13 min read
Search an element in a sorted and rotated array with duplicates
Given a sorted and rotated array arr[] and a key, the task is to find whether key exists in the rotated array (with duplicates).Examples: Input: arr[] = [3, 3, 3, 1, 2, 3], key = 3 Output: trueExplanation: arr[0] = 3Input: arr[] = {3, 3, 3, 1, 2, 3}, key = 11 Output: falseExplanation: 11 is not pres
6 min read
Find missing elements from an Array with duplicates
Given an array arr[] of size N having integers in the range [1, N] with some of the elements missing. The task is to find the missing elements. Note: There can be duplicates in the array. Examples: Input: arr[] = {1, 3, 3, 3, 5}, N = 5Output: 2 4Explanation: The numbers missing from the list are 2 a
12 min read
Find duplicates in an Array with values 1 to N using counting sort
Given a constant array of N elements which contain elements from 1 to N - 1, with any of these numbers appearing any number of times.Examples: Input: N = 5, arr[] = {1, 3, 4, 2, 2} Output: 2 Explanation: 2 is the number occurring more than once.Input: N = 5, arr[] = {3, 1, 3, 4, 2} Output: 3 Explana
11 min read
Find duplicates in constant array with elements 0 to N-1 in O(1) space
Given a constant array of n elements which contains elements from 1 to n-1, with any of these numbers appearing any number of times. Find any one of these repeating numbers in O(n) and using only constant memory space. Examples: Input : arr[] = {1, 2, 3, 4, 5, 6, 3} Output : 3 As the given array is
12 min read
Find duplicate in an array in O(n) and by using O(1) extra space
Given an array arr[] containing n integers where each integer is between 1 and (n-1) (inclusive). There is only one duplicate element, find the duplicate element in O(n) time complexity and O(1) space.Examples : Input : arr[] = {1, 4, 3, 4, 2} Output : 4Input : arr[] = {1, 3, 2, 1}Output : 1Approach
13 min read
Find index of an extra element present in one sorted array
Given two sorted arrays. There is only 1 difference between the arrays. The first array has one element extra added in between. Find the index of the extra element. Examples: Input: {2, 4, 6, 8, 9, 10, 12}; {2, 4, 6, 8, 10, 12}; Output: 4 Explanation: The first array has an extra element 9. The extr
15+ min read
Find the Middle Element of an Array or List
Given an array or a list of elements. The task is to find the middle element of given array or list of elements. If the array size is odd, return the single middle element. If the array size is even, return the two middle elements. ExampleInput: arr = {1, 2, 3, 4, 5}Output: 3 Input: arr = {7, 8, 9,
9 min read