fork(1) 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. static boolean containsOnlyNegatives(int[] integers) {
  11. int result = -1;
  12. for(int i : integers) {
  13. // bitwise AND to keep the negative sign-bit
  14. result &= i;
  15. }
  16. var sign = Integer.signum(result);
  17. System.out.println(sign);
  18. return sign == -1;
  19. }
  20. public static void main (String[] args) throws java.lang.Exception
  21. {
  22. final int[] mixed = {-1, -2, 3, 0};
  23. final int[] negatives = {-1, -2};
  24. final int[] positives = {3, 4};
  25. var testCases = Map.of(
  26. "mixed", mixed
  27. , "only negatives", negatives
  28. , "only positives", positives
  29. );
  30.  
  31. testCases.forEach((k, v) ->
  32. System.out.println("Case: " + k + " containsOnlyNegatives: " + containsOnlyNegatives(v))
  33. );
  34. }
  35. }
Success #stdin #stdout 0.16s 36536KB
stdin
Standard input is empty
stdout
0
Case: only positives containsOnlyNegatives: false
0
Case: mixed containsOnlyNegatives: false
-1
Case: only negatives containsOnlyNegatives: true