fork(4) download
  1. // By Desi QnA
  2. import java.util.*;
  3.  
  4. public class Main {
  5. public static void main(String[] args) {
  6. int n = 9;
  7. int[] a = {1, 3, 3, 4, 1, 4, 4, 4, 4};
  8. int[] b = new int[10];
  9. int i = 0;
  10. while (i < n) {
  11. int x = a[i];
  12. b[x]++;
  13. i++;
  14. }
  15. // Above for loop took O(n) time.
  16. // All frequencies have been calculated in advance by now.. :-) :-)
  17.  
  18. int[] queries = {3, 4, 1};
  19. int q = 3;
  20. int j = 0;
  21. while (j < q) {
  22. int x = queries[j];
  23. System.out.print(b[x]); // takes O(1) time..
  24. System.out.print(' ');
  25. j++;
  26. }
  27. // Above for loop took O(q) time.
  28.  
  29. // Hence total time: O(n+q).
  30. }
  31. }
  32.  
Success #stdin #stdout 0.09s 47256KB
stdin
Standard input is empty
stdout
2 5 2