

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Equal (or Middle) Point in a sorted array with duplicates in C++
Suppose we have one sorted array with n elements. The array is sorted. We have to find whether an element exists in an array from where the number of smaller element is same as the number of larger elements. If the equal point appears multiple times in the array, return the index of first occurrence. If no such point is present, then return -1. Suppose the elements are like A = [1, 1, 2, 3, 3, 3, 3, 3], then the equal point is at index 2, the element is A[2] = 2. As it has only one smaller element that is 1, and only one larger element, that is 3.
We will create one auxiliary array to store all distinct elements in it. If the count of distinct elements is even, then we cannot find any equal point, otherwise the middle element will be the mid-point.
Example
#include<iostream> using namespace std; int searchEqualPoint(int arr[], int n) { int aux_arr[n]; int i = 0, aux_index = 0; while (i < n) { aux_arr[aux_index++] = i++; while (i<n && arr[i] == arr[i-1]) i++; } return (aux_index & 1)? aux_arr[aux_index>>1] : -1; } int main() { int arr[] = {1, 1, 2, 3, 3, 3, 3, 3}; int n = sizeof(arr)/sizeof(arr[0]); int index = searchEqualPoint(arr, n); if (index != -1) cout << "Equal Point is: " << arr[index]; else cout << "No Equal Point exists"; }
Output
Equal Point is: 2
- Related Questions & Answers
- Find a Fixed Point in an array with duplicates allowed in C++
- Remove Duplicates from Sorted Array in Python
- Remove Duplicates from Sorted Array II in C++
- Find a Fixed Point (Value equal to index) in a given array in C++
- Removing duplicates from a sorted array of literals in JavaScript
- Count of smaller or equal elements in the sorted array in C++
- Find element in a sorted array whose frequency is greater than or equal to n/2 in C++.
- Find a Fixed Point (Value equal to index) in a given array in C++ Program
- Find middle point segment from given segment lengths in C++
- Count elements less than or equal to a given value in a sorted rotated array in C++
- Find an equal point in a string of brackets using C++.
- Find a partition point in array in C++
- How to find middle element in a array in android?
- Print distinct sorted permutations with duplicates allowed in input in C++
- Remove Duplicates from Sorted List in C++