-
Notifications
You must be signed in to change notification settings - Fork 1
/
twoTwo.java
53 lines (42 loc) · 858 Bytes
/
twoTwo.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
45
46
47
48
49
50
51
52
53
public boolean twoTwo(int[] nums) {
boolean flag=true;
if(nums[0]==2){
for(int i=nums.length-1;i>1;i--){
if(nums[i]==2 && nums[i-1]!=2)
flag=false;
}
return flag;}
else{
for(int i=nums.length-1;i>1;i--){
if(nums[i]==2 && nums[i-1]!=2)
flag=false;
}
if(flag==true && nums[1]==2 && nums[2]!=2)
flag=false;
return flag;
}
}
// Another Method
public boolean twoTwo(int[] nums) {boolean b=false;
for(int i=0;i<nums.length-1;i++)
{
if(nums[i+1]==2&&nums[i]==2)
b=true;
if(nums[i+1]==2&&nums[i]!=2)
b=false;
}
return b;
}
//Another Method
public boolean twoTwo(int[] nums) {
if((nums[0]==2 && nums[1]!=2) || (nums[nums.length-1]==2 && nums[nums.length-2]!=2))
return false;
for(int i=1;i<nums.length-1;i++)
{
if(nums[i]==2 && !(nums[i+1]==2 || nums[i-1]==2) )
{
return false;
}
}
return true;
}