-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo14.java
44 lines (38 loc) · 1.24 KB
/
No14.java
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
package Array02;
import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class No14 {
/*
* Given an array of ints, return true if it contains no 1's or it contains no 4's.
*
* no14([1, 2, 3]) → true
* no14([1, 2, 3, 4]) → false
* no14([2, 3, 4]) → true
*
* */
public static boolean no14(int[] nums) {
boolean two = false,four = false;
// Checking arrays with 1 element
for(int count = 0;count <nums.length; count++) {
if (nums[count] == 1)
two = true;
if(nums[count] == 4)
four = true;
}
if(nums.length == 0 || nums.length == 1)
return true;
else if (two ==true && four ==true)
return false;
else if (two || four)
return true;
else
return false;
}
public static void main(String[] args) {
System.out.println(no14(new int[]{1,2,3,4}));// should be false
System.out.println(no14(new int[]{1,2,3}));// should be true
System.out.println(no14(new int[]{2,3,4}));// should be true
System.out.println(no14(new int[]{2,3,4,1}));// should be false
}
}