Count of array elements that can be found using Randomized Binary Search on every array element
Last Updated :
29 May, 2021
Given an array arr[] of size N, the task is to find the minimum count of array elements found by applying the Randomized Binary Search for each array elements.
Examples:
Input: arr[] = { 5, 4, 9 }
Output: 2
Explanation:
Applying Randomized Binary Search for arr[0] in the array.
Initially, search space is [0, 2]
Suppose pivot = 1 and arr[pivot] < arr[0]. Therefore, the new search space is [2, 2] and arr[0] not found in the search space.
For arr[1], search space is [0, 2].
Suppose pivot = 0 and arr[pivot] > arr[0]. Therefore, the new search space is [0, 0] and arr[1] not found in the search space.
For arr[2], search space is [0, 2].
Selecting any element as pivot, arr[2] can be found.
Input: arr[] = { 1, 2, 3, 4 }
Output: 4
Approach: The idea is to count the array elements before which all array elements are smaller than it, and after which all are greater than it. Follow the steps below to solve the problem:
- Initialize an array, say smallestRight[] to store the smallest element on the right side of each array element.
- Traverse the array in reverse and update smallestRight[i] = min(smallestRight[ i + 1], arr[i]).
- Traverse the array and store the largest element on the left side of each array element and check if the largest element on the left side is less than the smallest element on the right side or not. If found to be true, then increment the count.
- Finally, print the count obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getDefiniteFinds(vector< int >& arr)
{
int n = arr.size();
vector< int > smallestRight(n + 1);
smallestRight[n] = INT_MAX;
for ( int i = n - 1; i >= 0; i--) {
smallestRight[i]
= min(smallestRight[i + 1], arr[i]);
}
int mn = INT_MIN;
int ans = 0;
for ( int i = 0; i < n; i++) {
if (mn < arr[i] and arr[i] < smallestRight[i + 1]) {
ans++;
}
mn = max(arr[i], mn);
}
return ans;
}
int main()
{
vector< int > arr = { 5, 4, 9 };
cout << getDefiniteFinds(arr) << endl;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static int getDefiniteFinds( int [] arr)
{
int n = arr.length;
int [] smallestRight = new int [n + 1 ];
smallestRight[n] = Integer.MAX_VALUE;
for ( int i = n - 1 ; i >= 0 ; i--)
{
smallestRight[i]
= Math.min(smallestRight[i + 1 ], arr[i]);
}
int mn = Integer.MIN_VALUE;
int ans = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (mn < arr[i]
&& arr[i] < smallestRight[i + 1 ])
{
ans++;
}
mn = Math.max(arr[i], mn);
}
return ans;
}
public static void main(String[] args)
{
int [] arr = new int [] { 5 , 4 , 9 };
System.out.println(getDefiniteFinds(arr));
}
}
|
Python3
import sys
def getDefiniteFinds(arr):
n = len (arr)
smallestRight = [ 0 ] * (n + 1 )
smallestRight[n] = sys.maxsize
for i in range (n - 1 , - 1 , - 1 ):
smallestRight[i] = min (
smallestRight[i + 1 ], arr[i])
mn = - sys.maxsize - 1
ans = 0
for i in range (n):
if (mn < arr[i] and
arr[i] < smallestRight[i + 1 ]):
ans + = 1
mn = max (arr[i], mn)
return ans
arr = [ 5 , 4 , 9 ]
print (getDefiniteFinds(arr))
|
C#
using System;
class GFG
{
static int getDefiniteFinds( int [] arr)
{
int n = arr.Length;
int [] smallestRight = new int [n + 1];
smallestRight[n] = Int32.MaxValue;
for ( int i = n - 1; i >= 0; i--)
{
smallestRight[i]
= Math.Min(smallestRight[i + 1], arr[i]);
}
int mn = Int32.MinValue;
int ans = 0;
for ( int i = 0; i < n; i++)
{
if (mn < arr[i]
&& arr[i] < smallestRight[i + 1])
{
ans++;
}
mn = Math.Max(arr[i], mn);
}
return ans;
}
static public void Main()
{
int [] arr = new int [] { 5, 4, 9 };
Console.WriteLine(getDefiniteFinds(arr));
}
}
|
Javascript
<script>
function getDefiniteFinds(arr)
{
let n = arr.length;
let smallestRight = new Array(n+1).fill(0);
smallestRight[n] = Number.MAX_VALUE;
for (let i = n - 1; i >= 0; i--)
{
smallestRight[i]
= Math.min(smallestRight[i + 1], arr[i]);
}
let mn = Number.MIN_VALUE;
let ans = 0;
for (let i = 0; i < n; i++)
{
if (mn < arr[i]
&& arr[i] < smallestRight[i + 1])
{
ans++;
}
mn = Math.max(arr[i], mn);
}
return ans;
}
let arr = [ 5, 4, 9 ];
document.write(getDefiniteFinds(arr));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...