fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6. int n;
  7. cin>>n;
  8.  
  9. //input an array
  10. //TC -- O(N)
  11. int arr[n];
  12. for(int i = 0 ; i< n ; i++){
  13. cin>> arr[i];
  14. }
  15. //input query
  16. int q;
  17. cin>>q;
  18. //for each input query traverse in the array and count the number of times an element appears
  19. //TC -- O(q)
  20. for(int i = 0 ; i< q; i++){
  21. int query;
  22. cin>>query;
  23.  
  24. int cnt = 0;
  25. //TC--O(N)
  26. for(int j = 0 ; j< n ; j++){
  27. if(arr[j]== query){
  28. cnt++;
  29. }
  30. }
  31. cout<<cnt<<" ";
  32. }
  33.  
  34. return 0;
  35. }
  36. // total time complexity = O(N + N*Q) = O(N*(1+Q)) ==== O(N*Q)
  37.  
Success #stdin #stdout 0s 5320KB
stdin
10
1 2 2 3 3 4 5 6 7 7 
3
1 2 3
stdout
1 2 2