Java > Array-2 > twoTwo (CodingBat Solution)

Problem:

Given an array of ints, return true if every 2 that appears in the array is next to another 2.

twoTwo({4, 2, 2, 3}) → true
twoTwo({2, 2, 4}) → true
twoTwo({2, 2, 4, 2}) → false


Solution:

public boolean twoTwo(int[] nums) {
int index =0;
for (int i=0; i<(nums.length); i++)
{
if(nums[i]==2)
{
i++;
if(!(i<(nums.length)) || nums[i] !=2) return false;
while(i<nums.length && nums[i] ==2) i++;
}
}
return true;
}


9 comments :

  1. static boolean twoTwo(int[] nums) {
    int len = nums.length;
    boolean result = true;
    boolean found = false;

    for(int i=0; i<len; i++){
    if(nums[i] !=2) found = false;

    if(nums[i] ==2 && found) result = true;

    if(nums[i] ==2 && !found){
    found = true;
    result = false;
    }

    }
    return result;
    }

    ReplyDelete
  2. public boolean twoTwo(int[] nums) {

    for (int i=0; i= 1 && nums[i-1] == 2)
    continue;
    if (i < (nums.length-1) && nums[i+1] == 2)
    continue;
    return false;
    }
    return true;

    ReplyDelete
  3. public boolean twoTwo(int[] nums) {
    if(nums.length == 0) {
    return true;
    }

    if(nums.length == 1 && nums[0] == 2)
    return false;

    if(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) {
    if(nums[i-1] != 2 && nums[i+1] != 2) {
    return false;
    }
    }
    }
    return true;
    }

    ReplyDelete
  4. public boolean twoTwo(int[] nums) {
    if (nums.length == 1 && nums[0] == 2)
    {
    return false;
    }
    for (int i = 0; i < nums.length - 1; i++)
    {
    if ((nums[i] == 2 && nums[i + 1] != 2) || (i == nums.length - 2 && nums[i] != 2 && nums[i + 1] == 2))
    {
    return false;
    }
    else if (nums[i] == 2 && nums[i + 1] == 2)
    {
    i++;
    }
    }
    return true;
    }

    ReplyDelete
  5. public boolean twoTwo(int[] nums) {
    int flag=0;
    int flag1=0;

    for(int i=0;inums.length-1 )
    {
    break;
    }
    else
    {
    if(nums[i+1]==2)
    {
    flag=1;
    }


    }


    }
    else
    {
    if(i+1> nums.length-1)
    {
    break;

    }
    else
    {
    if(nums[i+1]==2)
    {
    flag=0;
    }
    }
    }

    }
    if(flag==1)
    return true;
    else if(flag1==0)
    return true;
    else
    return false;
    }

    ReplyDelete
  6. public boolean twoTwo(int[] nums)
    {
    if(nums.length == 1 && nums[0] == 2)
    return false;

    for(int i = 0; i<nums.length ; i++)
    {
    if(nums[i] == 2)
    {
    if(i+1 < nums.length && nums[i+1] != 2)
    {
    return false;
    }
    if(i == nums.length-1 && nums[i-1] != 2 ) // only for last element
    {
    return false;
    }
    else
    {
    i += 1;
    }
    }
    }

    return true;
    }

    ReplyDelete
  7. public boolean twoTwo(int[] nums) {
    int len = nums.length;

    for (int i = 0; i < len; i++) {
    if (nums[i] == 2) {
    i++;
    if (i == len || nums[i] != 2) {
    return false;
    } else {
    i++;
    }
    }
    }
    return true;

    }

    ReplyDelete
  8. MORE EASY TO UNDERISTAND



    public boolean twoTwo(int[] nums) {
    int count=0;

    for(int i=0;i1||count==0;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact