# Count number of elements between two given elements in array

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

`// Program to count number of elements between ` `// two given elements. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count number of elements ` `// occurs between the elements. ` `int` `getCount(` `int` `arr[], ` `int` `n, ` `int` `num1, ` `int` `num2) ` `{ ` ` ` `// Find num1 ` ` ` `int` `i = 0; ` ` ` `for` `(i = 0; i < n; i++) ` ` ` `if` `(arr[i] == num1) ` ` ` `break` `; ` ` ` ` ` `// If num1 is not present or present at end ` ` ` `if` `(i >= n-1) ` ` ` `return` `0; ` ` ` ` ` `// Find num2 ` ` ` `int` `j; ` ` ` `for` `(j = n-1; j >= i+1; j--) ` ` ` `if` `(arr[j] == num2) ` ` ` `break` `; ` ` ` ` ` `// If num2 is not present ` ` ` `if` `(j == i) ` ` ` `return` `0; ` ` ` ` ` `// return number of elements between ` ` ` `// the two elements. ` ` ` `return` `(j - i - 1); ` `} ` ` ` `// Driver Code ` `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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Program to count number of elements ` `// between two given elements. ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to count number of elements ` ` ` `// occurs between the elements. ` ` ` `static` `int` `getCount(` `int` `arr[], ` `int` `n, ` ` ` `int` `num1, ` `int` `num2) ` ` ` `{ ` ` ` `// Find num1 ` ` ` `int` `i = ` `0` `; ` ` ` `for` `(i = ` `0` `; i < n; i++) ` ` ` `if` `(arr[i] == num1) ` ` ` `break` `; ` ` ` ` ` `// If num1 is not present ` ` ` `// or present at end ` ` ` `if` `(i >= n - ` `1` `) ` ` ` `return` `0` `; ` ` ` ` ` `// Find num2 ` ` ` `int` `j; ` ` ` `for` `(j = n - ` `1` `; j >= i + ` `1` `; j--) ` ` ` `if` `(arr[j] == num2) ` ` ` `break` `; ` ` ` ` ` `// If num2 is not present ` ` ` `if` `(j == i) ` ` ` `return` `0` `; ` ` ` ` ` `// return number of elements ` ` ` `// between the two elements. ` ` ` `return` `(j - i - ` `1` `); ` ` ` `} ` ` ` ` ` `// Drvier program ` ` ` `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)); ` ` ` ` ` `} ` `} ` `// This code is contributed by vt_m ` |

*chevron_right*

*filter_none*

## Python3

`# Python Program to count number of elements between ` `# two given elements. ` ` ` `# Function to count number of elements ` `# occurs between the elements. ` `def` `getCount(arr, n, num1, num2): ` ` ` ` ` `# Find num1 ` ` ` `for` `i ` `in` `range` `(` `0` `,n): ` ` ` `if` `(arr[i] ` `=` `=` `num1): ` ` ` `break` ` ` ` ` `#If num1 is not present or present at end ` ` ` `if` `(i >` `=` `n` `-` `1` `): ` ` ` `return` `0` ` ` ` ` `# Find num2 ` ` ` `for` `j ` `in` `range` `(n` `-` `1` `, i` `+` `1` `, ` `-` `1` `): ` ` ` `if` `(arr[j] ` `=` `=` `num2): ` ` ` `break` ` ` ` ` `# If num2 is not present ` ` ` `if` `(j ` `=` `=` `i): ` ` ` `return` `0` ` ` ` ` `# return number of elements between ` ` ` `# the two elements. ` ` ` `return` `(j ` `-` `i ` `-` `1` `) ` ` ` `# Driver Code ` `arr` `=` `[ ` `3` `, ` `5` `, ` `7` `, ` `6` `, ` `4` `, ` `9` `, ` `12` `, ` `4` `, ` `8` `] ` `n` `=` `len` `(arr) ` `num1 ` `=` `5` `num2 ` `=` `4` `print` `(getCount(arr, n, num1, num2)) ` ` ` `# This code is contributed by SHARIQ_JMI ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to count number of elements ` `// between two given elements. ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to count number of elements ` ` ` `// occurs between the elements. ` ` ` `static` `int` `getCount(` `int` `[]arr, ` `int` `n, ` ` ` `int` `num1, ` `int` `num2) ` ` ` `{ ` ` ` ` ` `// Find num1 ` ` ` `int` `i = 0; ` ` ` `for` `(i = 0; i < n; i++) ` ` ` `if` `(arr[i] == num1) ` ` ` `break` `; ` ` ` ` ` `// If num1 is not present ` ` ` `// or present at end ` ` ` `if` `(i >= n - 1) ` ` ` `return` `0; ` ` ` ` ` `// Find num2 ` ` ` `int` `j; ` ` ` `for` `(j = n - 1; j >= i + 1; j--) ` ` ` `if` `(arr[j] == num2) ` ` ` `break` `; ` ` ` ` ` `// If num2 is not present ` ` ` `if` `(j == i) ` ` ` `return` `0; ` ` ` ` ` `// return number of elements ` ` ` `// between the two elements. ` ` ` `return` `(j - i - 1); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `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)); ` ` ` ` ` `} ` `} ` ` ` `// This article is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// Program to count number ` `// of elements between ` `// two given elements. ` ` ` `// Function to count ` `// number of elements ` `// occurs between the ` `// elements. ` `function` `getCount(` `$arr` `, ` `$n` `, ` ` ` `$num1` `, ` `$num2` `) ` `{ ` ` ` ` ` `// Find num1 ` ` ` `$i` `= 0; ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `if` `(` `$arr` `[` `$i` `] == ` `$num1` `) ` ` ` `break` `; ` ` ` ` ` `// If num1 is not present ` ` ` `// or present at end ` ` ` `if` `(` `$i` `>= ` `$n` `- 1) ` ` ` `return` `0; ` ` ` ` ` `// Find num2 ` ` ` `$j` `; ` ` ` `for` `(` `$j` `= ` `$n` `- 1; ` `$j` `>= ` `$i` `+ 1; ` `$j` `--) ` ` ` `if` `(` `$arr` `[` `$j` `] == ` `$num2` `) ` ` ` `break` `; ` ` ` ` ` `// If num2 is not present ` ` ` `if` `(` `$j` `== ` `$i` `) ` ` ` `return` `0; ` ` ` ` ` `// return number of elements ` ` ` `// betweenthe two elements. ` ` ` `return` `(` `$j` `- ` `$i` `- 1); ` `} ` ` ` `// Driver Code ` `$arr` `= ` `array` `(3, 5, 7, 6, 4, 9, 12, 4, 8); ` `$n` `= sizeof(` `$arr` `); ` `$num1` `= 5; ` `$num2` `= 4; ` `echo` `(getCount(` `$arr` `, ` `$n` `, ` `$num1` `, ` `$num2` `)); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

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 contribute.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.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count of Array elements to be divided by 2 to make at least K elements equal
- Count number of even and odd elements in an array
- Count of array elements which is smaller than both its adjacent elements
- Count number of even and odd length elements in an Array
- Count the number of elements in an array which are divisible by k
- Count the maximum number of elements that can be selected from the array
- Count occurrences of the average of array elements with a given number
- Count array elements that divide the sum of all other elements
- Count of elements not divisible by any other elements of Array
- Count the number of elements which are greater than any of element on right side of an array
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Count subarrays such that remainder after dividing sum of elements by K gives count of elements
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Count of elements in first Array greater than second Array with each element considered only once
- Count of elements whose absolute difference with the sum of all the other elements is greater than k
- Count elements such that there are exactly X elements with values greater than or equal to X
- Count of elements which is the sum of a subarray of the given Array
- Count of even and odd set bit Array elements after XOR with K for Q queries
- Count of elements A[i] such that A[i] + 1 is also present in the Array