fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main(String[] args) {
  11. int count = countOdd(new Integer [] {5, 7, 3, 9, 0});
  12. System.out.println(count);
  13. count = countOdd(new Integer [] {2, 6, 4, 8, 1});
  14. System.out.println(count);
  15. count = countOdd(5, 7, 10);
  16. System.out.println(count);
  17. count = countOdd(8, 2, 7);
  18. System.out.println(count);
  19. count = countOdd(new Integer[][] {{1, 2}, {3, 4, 5}});//extra point only
  20. System.out.println(count);
  21. count = countOdd(new Integer[][] {{6, 2}, {3, 4, 0}});//extra point only
  22. System.out.println(count);
  23. count = countOdd(new Integer [] {2, 6, 4, 8, 1}, new Integer [] {3, 5, 7, 8, 1});
  24. System.out.println(count);
  25. }
  26.  
  27. // The var args version. You call this. It then calls the recursive
  28. // version.
  29. public static <T> int countOdd(T... arguments)
  30. {
  31. return countOddRec(arguments);
  32. }
  33.  
  34. // Recursive version
  35. private static <T> int countOddRec(T[] a)
  36. {
  37. if (a == null || a.length == 0) return 0;
  38.  
  39. int count=0;
  40.  
  41. // Is it an array of Numbers?
  42. if (a[0] instanceof Number) {
  43. for (T i: a) {
  44. // Simplified the counting code a bit. Any # mod 2 is either 0 or 1
  45. count += ((Number)i).intValue() % 2;
  46. }
  47. }
  48. // Is it an multi-dimensional? Call recursively for each sub-array.
  49. else {
  50. for (T sub : a) {
  51. count += countOddRec((T[])sub);
  52. }
  53. }
  54.  
  55. return count;
  56. }
  57. }
Success #stdin #stdout 0.09s 46996KB
stdin
Standard input is empty
stdout
4
1
2
1
3
1
5