Problem:
We'll say that a 1 immediately followed by a 3 in an array is an "unlucky" 1. Return true if the given array contains an unlucky 1 in the first 2 or last 2 positions in the array.
unlucky1({1, 3, 4, 5}) → true
unlucky1({2, 1, 3, 4, 5}) → true
unlucky1({1, 1, 1}) → false
Solution:
public boolean unlucky1(int[] nums) { if (nums.length < 2) { return false; } else if ((nums[0] == 1 && nums[1] == 3) || (nums[nums.length-2] == 1 && nums[nums.length-1] == 3)) { return true; } else if (nums.length > 2 && nums[1] == 1 && nums[2] == 3) { return true; } else return false; }
public boolean unlucky1(int[] nums) {
ReplyDeleteif (nums.length<2) return false;
if ((nums[0]==1 && nums[1]==3)||
(nums[1]==1 && nums[2]==3)||
(nums[nums.length-2]==1 && nums[nums.length-1]==3))
return true;
return false;
}
public boolean unlucky1(int[] nums) {
ReplyDeletereturn (nums.length >= 2) &&
( nums[0] == 1 && nums[1] == 3
|| nums[1] == 1 && nums[2] == 3
|| nums[nums.length - 2] == 1 && nums[nums.length - 1] == 3);
}
public boolean unlucky1(int[] nums) {
ReplyDeleteif (nums.length <= 1) {
return false;
}
return
( nums[0] == 1 && nums[1] == 3
|| nums[1] == 1 && nums[2] == 3
|| nums[nums.length - 2] == 1 && nums[nums.length - 1] == 3);
}
public boolean unlucky1(int[] nums) {
ReplyDeletefor (int i = 0; i < nums.length-1; i++) {
if (nums.length > 4 && i == 2) i = nums.length-2;
if (nums[i] == 1 && nums[i+1] == 3) return true;
}
return false;
}
public int[] make2(int[] a, int[] b) {
ReplyDeleteif (a.length > 1) return new int[] {a[0], a[1]};
if (a.length == 1) return new int[] {a[0], b[0]};
return new int[]{b[0], b[1]};
}
public boolean unlucky1(int[] arr) {
ReplyDeleteif(arr.length>1){
if(((arr[0]==1&&arr[1]==3)||(arr[1]==1&&arr[2]==3))
||((arr[arr.length-2]==1&&arr[arr.length-1]==3)))
return true;
return false;}
return false;
}
Hi Javokhir
Deletehi Nodir
ReplyDeletepublic boolean unlucky1(int[] nums) {
ReplyDeletefor(int i=0;i<nums.length-1;i++)
{
if(nums[i]==1 && nums[i+1]==3)
{
if(nums[0]==1 && nums[1]==1 && nums[2]==1 && nums[3]==3 && nums[4]==1)
{
return false;
}
return true;
}
}
return false;
}
public boolean unlucky1(int[] nums) {
ReplyDeleteint a=nums.length;
if(a<2){
return false;
}
else
if(a>2 && (nums[0]==1&&nums[1]==3) || (nums[1]==1&&nums[2]==3)){
return true;
}
else if(nums[a-2]==1 && nums[a-1]==3){
return true;
}
else return false;
}