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) {
  boolean isTrue = false;
  
  for (int i = 0; i < nums.length; i++) {
    if (nums[i] == 2) {
      if (nums.length > 1 && i < nums.length-1 && nums[i+1] == 2)
        isTrue = true;
      else if (nums.length > 1 && i > 0 && nums[i-1] == 2)
        isTrue = true;
      else
        return false;
    }  
  }
  return true;
}


7 comments :

  1. public boolean twoTwo(int[] nums) {
    int[] nums1 = new int[nums.length + 2];

    for (int i = 0; i < nums.length; i++) {
    nums1[i+1] = nums[i];
    }

    for (int i = 0; i < nums1.length ; i++) {
    if (nums1[i] == 2) {
    if (!(nums1[i+1] == 2 || nums1[i-1] == 2)) return false;
    }


    }
    return true;
    }

    ReplyDelete
  2. A little bit long but works))

    public static boolean twoTwo(int[] nums) {
    boolean twice = true;
    if(nums.length>1 && nums[0]==2 && nums[1]!=2){
    return false;
    }
    if(nums.length>1 && 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){
    twice = false;
    }
    }

    }
    return twice;

    }

    ReplyDelete
    Replies
    1. //added if array length is 1 with value 2
      public boolean twoTwo(int[] nums) {
      boolean twice = true;

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

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

      if(nums.length>1 && 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){
      twice = false;
      }
      }
      }

      return twice;
      }

      Delete
  3. public boolean twoTwo(int[] nums) {
    int count=0;//دا بيعد كل اتنين ورا بعض بواحد
    int count2=0;//دا بيعد عدد الاتنين في الاراي
    for(int i=0;i<nums.length;i++){
    if(nums[i]==2)count2++;
    if(i<nums.length-1 && nums[i]==2&&nums[i+1]==2){count++;
    }

    }
    return(count2==count*2 || count2==3&&count==2);//حاله خاصه اللى بعد ال or
    }

    ReplyDelete
  4. public boolean twoTwo(int[] nums) {
    boolean twice = true;

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

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

    if(nums.length>1 && 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){
    twice = false;
    }
    }
    }

    return twice;
    }

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

    int count=0;

    for(int i=0; i0 && nums[i-1]==2) || (i=0 && count%2==0);
    }

    ReplyDelete

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

    for (int i = 0; i < nums.length; i++)
    {
    if (nums[i] == 2)
    {
    if ((i > 0 && nums[i - 1] == 2)
    || (i < nums.length - 1 && nums[i + 1] == 2))
    {
    count += 2;
    i++;
    }
    else
    {
    count = count - 1;
    }
    }
    }
    return (count >= 0 && count % 2 == 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