Given an unsorted array of n elements and also given two points num1 and num2. The task is to count number of elements occurs between the given points (excluding num1 and num2).
If there are multiple occurrences of num1 and num2, we need to consider leftmost occurrence of num1 and rightmost occurrence of num2.
Examples:
Input : arr[] = {3 5 7 6 4 9 12 4 8}
num1 = 5
num2 = 4
Output : 5
Number of elements between leftmost occurrence
of 5 and rightmost occurrence of 4 is five.
Input : arr[] = {4, 6, 8, 3, 6, 2, 8, 9, 4}
num1 = 4
num2 = 4
Output : 7
Input : arr[] = {4, 6, 8, 3, 6, 2, 8, 9, 4}
num1 = 4
num2 = 10
Output : 0
The solution should traverse array only once in all cases (when single or both elements are not present)
The idea is to traverse array from left and find first occurrence of num1. If we reach end, we return 0. Then we traverse from rightmost element and find num2. We traverse only till the point which is greater than index of num1. If we reach end, we return 0. If we found both elements, we return count using indexes of found elements.
CPP
#include <bits/stdc++.h>
using namespace std;
int getCount( int arr[], int n, int num1, int num2)
{
int i = 0;
for (i = 0; i < n; i++)
if (arr[i] == num1)
break ;
if (i >= n-1)
return 0;
int j;
for (j = n-1; j >= i+1; j--)
if (arr[j] == num2)
break ;
if (j == i)
return 0;
return (j - i - 1);
}
int main()
{
int arr[] = { 3, 5, 7, 6, 4, 9, 12, 4, 8 };
int n = sizeof (arr) / sizeof (arr[0]);
int num1 = 5, num2 = 4;
cout << getCount(arr, n, num1, num2);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int getCount( int arr[], int n,
int num1, int num2)
{
int i = 0 ;
for (i = 0 ; i < n; i++)
if (arr[i] == num1)
break ;
if (i >= n - 1 )
return 0 ;
int j;
for (j = n - 1 ; j >= i + 1 ; j--)
if (arr[j] == num2)
break ;
if (j == i)
return 0 ;
return (j - i - 1 );
}
public static void main (String[] args)
{
int arr[] = { 3 , 5 , 7 , 6 , 4 , 9 , 12 , 4 , 8 };
int n = arr.length;
int num1 = 5 , num2 = 4 ;
System.out.println( getCount(arr, n, num1, num2));
}
}
|
Python3
def getCount(arr, n, num1, num2):
for i in range ( 0 ,n):
if (arr[i] = = num1):
break
if (i > = n - 1 ):
return 0
for j in range (n - 1 , i + 1 , - 1 ):
if (arr[j] = = num2):
break
if (j = = i):
return 0
return (j - i - 1 )
arr = [ 3 , 5 , 7 , 6 , 4 , 9 , 12 , 4 , 8 ]
n = len (arr)
num1 = 5
num2 = 4
print (getCount(arr, n, num1, num2))
|
C#
using System;
class GFG {
static int getCount( int []arr, int n,
int num1, int num2)
{
int i = 0;
for (i = 0; i < n; i++)
if (arr[i] == num1)
break ;
if (i >= n - 1)
return 0;
int j;
for (j = n - 1; j >= i + 1; j--)
if (arr[j] == num2)
break ;
if (j == i)
return 0;
return (j - i - 1);
}
public static void Main ()
{
int []arr = {3, 5, 7, 6, 4, 9, 12, 4, 8};
int n = arr.Length;
int num1 = 5, num2 = 4;
Console.WriteLine(getCount(arr, n, num1, num2));
}
}
|
PHP
<?php
function getCount( $arr , $n ,
$num1 , $num2 )
{
$i = 0;
for ( $i = 0; $i < $n ; $i ++)
if ( $arr [ $i ] == $num1 )
break ;
if ( $i >= $n - 1)
return 0;
$j ;
for ( $j = $n - 1; $j >= $i + 1; $j --)
if ( $arr [ $j ] == $num2 )
break ;
if ( $j == $i )
return 0;
return ( $j - $i - 1);
}
$arr = array (3, 5, 7, 6, 4, 9, 12, 4, 8);
$n = sizeof( $arr );
$num1 = 5; $num2 = 4;
echo (getCount( $arr , $n , $num1 , $num2 ));
?>
|
Javascript
<script>
function getCount( arr, n, num1, num2)
{
let i = 0;
for (i = 0; i < n; i++)
if (arr[i] == num1)
break ;
if (i >= n-1)
return 0;
let j;
for (j = n-1; j >= i+1; j--)
if (arr[j] == num2)
break ;
if (j == i)
return 0;
return (j - i - 1);
}
let arr = [ 3, 5, 7, 6, 4, 9, 12, 4, 8 ];
let n = arr.length;
let num1 = 5, num2 = 4;
document.write(getCount(arr, n, num1, num2));
</script>
|
Output:
5
How to handle multiple queries?
For handling multiple queries, we can use hashing and store leftmost and rightmost indexes for every element present in array. Once we have stored these, we can answer all queries in O(1) time.
This article is contributed by Dharmendra kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.