Given an unsorted array of size n, find no of elements between two elements i and j (both inclusive).
Examples:
Input : arr = [1 3 3 9 10 4]
i1 = 1, j1 = 4
i2 = 9, j2 = 12
Output : 4
2
The numbers are: 1 3 3 4 for first query
The numbers are: 9 10 for second querySource: Amazon Interview Experience
Become a success story instead of just reading about them. Prepare for coding interviews at Amazon and other top product-based companies with our Amazon Test Series. Includes topic-wise practice questions on all important DSA topics along with 10 practice contests of 2 hours each. Designed by industry experts that will surely help you practice and sharpen your programming skills. Wait no more, start your preparation today!
A simple approach will be to run a for loop to check if each element is in the given range and maintain their count. Time complexity for running each query will be O(n).
C++
#include <bits/stdc++.h>
using namespace std;
int countInRange(int arr[], int n, int x, int y)
{
int count = 0;
for (int i = 0; i < n; i++) {
if (arr[i] >= x && arr[i] <= y)
count++;
}
return count;
}
int main()
{
int arr[] = { 1, 3, 4, 9, 10, 3 };
int n = sizeof(arr) / sizeof(arr[0]);
int i = 1, j = 4;
cout << countInRange(arr, n, i, j) << endl;
i = 9, j = 12;
cout << countInRange(arr, n, i, j) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int countInRange(int arr[], int n, int x, int y)
{
int count = 0;
for (int i = 0; i < n; i++) {
if (arr[i] >= x && arr[i] <= y)
count++;
}
return count;
}
public static void main (String[] args)
{
int arr[] = { 1, 3, 4, 9, 10, 3 };
int n = arr.length;
int i = 1, j = 4;
System.out.println ( countInRange(arr, n, i, j)) ;
i = 9;
j = 12;
System.out.println ( countInRange(arr, n, i, j)) ;
}
}
|
Python
def countInRange(arr, n, x, y):
count = 0;
for i in range(n):
if (arr[i] >= x and arr[i] <= y):
count += 1
return count
arr = [1, 3, 4, 9, 10, 3]
n = len(arr)
i = 1
j = 4
print(countInRange(arr, n, i, j))
i = 9
j = 12
print(countInRange(arr, n, i, j))
|
C#
using System;
class GFG {
static int countInRange(int []arr, int n,
int x, int y)
{
int count = 0;
for (int i = 0; i < n; i++) {
if (arr[i] >= x && arr[i] <= y)
count++;
}
return count;
}
public static void Main ()
{
int[]arr = {1, 3, 4, 9, 10, 3};
int n = arr.Length;
int i = 1, j = 4;
Console.WriteLine( countInRange(arr, n, i, j)) ;
i = 9;
j = 12;
Console.WriteLine( countInRange(arr, n, i, j)) ;
}
}
|
PHP
<?php
function countInRange($arr, $n,
$x, $y)
{
$count = 0;
for ($i = 0; $i < $n; $i++)
{
if ($arr[$i] >= $x &&
$arr[$i] <= $y)
$count++;
}
return $count;
}
$arr = array(1, 3, 4, 9, 10, 3);
$n = count($arr);
$i = 1;
$j = 4;
echo countInRange($arr, $n, $i, $j)."\n";
$i = 9;
$j = 12;
echo countInRange($arr, $n, $i, $j)."\n";
?>
|
Javascript
<script>
function countInRange(arr, n, x, y)
{
let count = 0;
for (let i = 0; i < n; i++) {
if (arr[i] >= x && arr[i] <= y)
count++;
}
return count;
}
let arr = [1, 3, 4, 9, 10, 3];
let n = arr.length;
let i = 1, j = 4;
document.write( countInRange(arr, n, i, j) + "</br>") ;
i = 9;
j = 12;
document.write( countInRange(arr, n, i, j)) ;
</script>
|
Output:
4
2
An Efficient Approach will be to first sort the array and then using a modified binary search function find two indices, one of first element greater than or equal to lower bound of range and the other of the last element less than or equal to upperbound. Time for running each query will be O(logn) and for sorting the array once will be O(nlogn).
C++
#include <bits/stdc++.h>
using namespace std;
int lowerIndex(int arr[], int n, int x)
{
int l = 0, h = n - 1;
while (l <= h) {
int mid = (l + h) / 2;
if (arr[mid] >= x)
h = mid - 1;
else
l = mid + 1;
}
return l;
}
int upperIndex(int arr[], int n, int y)
{
int l = 0, h = n - 1;
while (l <= h) {
int mid = (l + h) / 2;
if (arr[mid] <= y)
l = mid + 1;
else
h = mid - 1;
}
return h;
}
int countInRange(int arr[], int n, int x, int y)
{
int count = 0;
count = upperIndex(arr, n, y) - lowerIndex(arr, n, x) + 1;
return count;
}
int main()
{
int arr[] = { 1, 4, 4, 9, 10, 3 };
int n = sizeof(arr) / sizeof(arr[0]);
sort(arr, arr + n);
int i = 1, j = 4;
cout << countInRange(arr, n, i, j) << endl;
i = 9, j = 12;
cout << countInRange(arr, n, i, j) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;
class GFG
{
static int lowerIndex(int arr[], int n, int x)
{
int l = 0, h = n - 1;
while (l <= h)
{
int mid = (l + h) / 2;
if (arr[mid] >= x)
h = mid - 1;
else
l = mid + 1;
}
return l;
}
static int upperIndex(int arr[], int n, int y)
{
int l = 0, h = n - 1;
while (l <= h)
{
int mid = (l + h) / 2;
if (arr[mid] <= y)
l = mid + 1;
else
h = mid - 1;
}
return h;
}
static int countInRange(int arr[], int n, int x, int y)
{
int count = 0;
count = upperIndex(arr, n, y) -
lowerIndex(arr, n, x) + 1;
return count;
}
public static void main (String[] args)
{
int arr[] = { 1, 4, 4, 9, 10, 3 };
int n = arr.length;
Arrays.sort(arr);
int i = 1, j = 4;
System.out.println( countInRange(arr, n, i, j)); ;
i = 9;
j = 12;
System.out.println( countInRange(arr, n, i, j));
}
}
|
Python
def lowerIndex(arr, n, x):
l = 0
h = n-1
while (l <= h):
mid = int((l + h)/2)
if (arr[mid] >= x):
h = mid - 1
else:
l = mid + 1
return l
def upperIndex(arr, n, x):
l = 0
h = n-1
while (l <= h):
mid = int((l + h)/2)
if (arr[mid] <= x):
l = mid + 1
else:
h = mid - 1
return h
def countInRange(arr, n, x, y):
count = 0;
count = upperIndex(arr, n, y) - lowerIndex(arr, n, x) + 1;
return count
arr = [1, 3, 4, 9, 10, 3]
arr.sort()
n = len(arr)
i = 1
j = 4
print(countInRange(arr, n, i, j))
i = 9
j = 12
print(countInRange(arr, n, i, j))
|
C#
using System;
class GFG
{
static int lowerIndex(int []arr, int n,
int x)
{
int l = 0, h = n - 1;
while (l <= h)
{
int mid = (l + h) / 2;
if (arr[mid] >= x)
h = mid - 1;
else
l = mid + 1;
}
return l;
}
static int upperIndex(int []arr, int n,
int y)
{
int l = 0, h = n - 1;
while (l <= h)
{
int mid = (l + h) / 2;
if (arr[mid] <= y)
l = mid + 1;
else
h = mid - 1;
}
return h;
}
static int countInRange(int []arr, int n,
int x, int y)
{
int count = 0;
count = upperIndex(arr, n, y) -
lowerIndex(arr, n, x) + 1;
return count;
}
public static void Main ()
{
int []arr = {1, 4, 4, 9, 10, 3};
int n = arr.Length;
Array.Sort(arr);
int i = 1, j = 4;
Console.WriteLine(countInRange(arr, n, i, j)); ;
i = 9;
j = 12;
Console.WriteLine(countInRange(arr, n, i, j));
}
}
|
PHP
<?php
function lowerIndex($arr, $n, $x)
{
$l = 0; $h = $n - 1;
while ($l <= $h)
{
$mid = ($l + $h) / 2;
if ($arr[$mid] >= $x)
$h = $mid - 1;
else
$l = $mid + 1;
}
return $l;
}
function upperIndex($arr, $n, $y)
{
$l = 0; $h = $n - 1;
while ($l <= $h)
{
$mid = ($l + $h) / 2;
if ($arr[$mid] <= $y)
$l = $mid + 1;
else
$h = $mid - 1;
}
return $h;
}
function countInRange($arr, $n, $x, $y)
{
$count = 0;
$count = (upperIndex($arr, $n, $y) -
lowerIndex($arr, $n, $x) + 1);
$t = floor($count);
return $t;
}
$arr = array( 1, 4, 4, 9, 10, 3 );
$n = sizeof($arr);
sort($arr);
$i = 1; $j = 4;
echo countInRange($arr, $n, $i, $j), "\n";
$i = 9; $j = 12;
echo countInRange($arr, $n, $i, $j), "\n";
?>
|
Javascript
<script>
function lowerIndex(arr, n, x)
{
let l = 0, h = n - 1;
while (l <= h)
{
let mid = parseInt((l + h) / 2, 10);
if (arr[mid] >= x)
h = mid - 1;
else
l = mid + 1;
}
return l;
}
function upperIndex(arr, n, y)
{
let l = 0, h = n - 1;
while (l <= h)
{
let mid = parseInt((l + h) / 2, 10);
if (arr[mid] <= y)
l = mid + 1;
else
h = mid - 1;
}
return h;
}
function countInRange(arr, n, x, y)
{
let count = 0;
count = upperIndex(arr, n, y) -
lowerIndex(arr, n, x) + 1;
return count;
}
let arr = [1, 4, 4, 9, 10, 3];
let n = arr.length;
arr.sort(function(a, b){return a - b});
let i = 1, j = 4;
document.write(countInRange(arr, n, i, j) + "</br>"); ;
i = 9;
j = 12;
document.write(countInRange(arr, n, i, j));
</script>
|
Output:
4
2
This article is contributed by Aditi Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.