-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOccurence_ofNumber_inArray_UsingRecursion.cpp
More file actions
49 lines (29 loc) · 1.32 KB
/
Copy pathOccurence_ofNumber_inArray_UsingRecursion.cpp
File metadata and controls
49 lines (29 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<bits/stdc++.h>
using namespace std;
// Lets solve the Occurence count of elements in Array using Recursion. We can solve this by using normal for loop also and also by using Hashing Technique. But here lets solve this using Recusion.
// Time Complexity of this one is also the same as normal while using for loop that is - O(Q*N) , where Q is number of queries and N is the size of Array.
int Occurence(int number, int count , int size , int arr[] , int i){
if(i==size) return count; // Base Condition
if(number == arr[i]) count++;
return Occurence(number, count , size, arr , i+1);
}
int main(){
int size;
cout<<"Enter the size of the Array you want: ";
cin>>size;
int arr[size];
//for input the values int the Array
cout<<"Enter the values in the Array now : ";
for(int i=0 ; i<size ; i++){
cin>>arr[i];
}
int q; // Number of queries
cout<<"How many elements counts you want to see: ";
cin>>q;
while(q--){
int number, count=0;
cout<<"Enter the number to see its count: ";
cin>>number;
cout<<"The Occurence of :"<<number<<" is :"<<Occurence(number, count , size , arr , 0)<<endl;
}
}