Skip to content

Commit 6a00544

Browse files
Create range.cpp
1 parent 83bed70 commit 6a00544

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

range.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// C++ program to find total count of an element
2+
// in a range
3+
#include<bits/stdc++.h>
4+
using namespace std;
5+
6+
// Returns count of element in arr[left-1..right-1]
7+
int findFrequency(int arr[], int n, int left,
8+
int right, int element)
9+
{
10+
int count = 0;
11+
for (int i=left-1; i<=right; ++i)
12+
if (arr[i] == element)
13+
++count;
14+
return count;
15+
}
16+
17+
// Driver Code
18+
int main()
19+
{
20+
int arr[] = {2, 8, 6, 9, 8, 6, 8, 2, 11};
21+
int n = sizeof(arr) / sizeof(arr[0]);
22+
23+
// Print frequency of 2 from position 1 to 6
24+
cout << "Frequency of 2 from 1 to 6 = "
25+
<< findFrequency(arr, n, 1, 6, 2) << endl;
26+
27+
// Print frequency of 8 from position 4 to 9
28+
cout << "Frequency of 8 from 4 to 9 = "
29+
<< findFrequency(arr, n, 4, 9, 8);
30+
31+
return 0;
32+
}

0 commit comments

Comments
 (0)