Java > Array-2 > either24 (CodingBat Solution)

Problem:

Given an array of ints, return true if the array contains a 2 next to a 2 or a 4 next to a 4, but not both.

either24({1, 2, 2}) → true
either24({4, 4, 1}) → true
either24({4, 4, 1, 2, 2}) → false


Solution:

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


17 comments :

  1. public boolean either24(int[] nums) {
    boolean esta = false;
    boolean dos = false;
    boolean cuatro = false;

    for(int i=0; i<nums.length-1; i++)
    {

    if(nums[i]==4 && nums[i+1]==4)
    {
    esta = true;
    cuatro=true;
    }

    if(nums[i]==2 && nums[i+1]==2)
    {
    esta = true;
    dos=true;
    }

    if(dos && cuatro)
    {
    esta = false;
    break;
    }
    }

    return esta;
    }

    ReplyDelete
  2. public boolean either24(int[] nums) {
    boolean fours=false;
    boolean twos=false;

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

    else if(nums[i]==4 && nums[i+1]==4){
    fours=true;
    }


    }

    if(fours && twos){
    return false;
    }
    if(!fours && !twos){
    return false;
    }

    else return true;

    }

    ReplyDelete
  3. I think that my solution is easier..
    public boolean either24(int[] nums) {
    boolean war2 = false;
    boolean war4 = false;

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

    ReplyDelete
  4. A better Version :
    public boolean either24(int[] nums) {
    boolean zwei =false;
    boolean vier = false;
    for(int i =0; i<nums.length - 1; i++){

    if(nums[i]== 2 && nums[i + 1]==2){
    zwei =true;
    }
    if(nums[i]== 4 && nums[i + 1]== 4){
    vier =true;
    }

    }
    if(vier==true ^ zwei == true){
    return true;
    }


    return false;

    }

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

    boolean two = false;
    boolean four = false;

    for(int i = 0; i < nums.length-1; i++) {

    if (nums[i] == 2 && nums[i+1] == 2) two = true;
    if (nums[i] == 4 && nums[i+1] == 4) four = true;

    }return two ^ four;

    }

    ReplyDelete
  6. if(Arrays.toString(nums).contains("2, 2")&&Arrays.toString(nums).contains("4, 4"))
    return false;
    if(Arrays.toString(nums).contains("2, 2")||Arrays.toString(nums).contains("4, 4"))
    return true;
    return false;

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

    ReplyDelete
  8. String arrS = Arrays.toString(nums).replaceAll("\\[|,|\\]|\\s","");
    boolean isTrue =
    (arrS.contains("22")&&!arrS.contains("44"))?true:
    (arrS.contains("44")&&!arrS.contains("22"))?true:false;
    return isTrue;

    ReplyDelete
  9. int c2=0,c4=0;
    for(int i=0;i0 && c4==0) || (c4>0 && c2==0);

    ReplyDelete
    Replies
    1. int c2=0,c4=0;
      for(int i=0;i0 && c4==0) || (c4>0 && c2==0);

      Delete
  10. doesn't let me to share whole code(?)

    ReplyDelete
  11. public boolean either24(int[] nums) {
    int four =0;
    int two= 0;
    for(int i=0; i0 && two==0) {
    return true;
    }

    else if(two>0 && four==0) {
    return true;
    }

    else{
    return false;
    }
    }

    ReplyDelete
  12. public boolean either24(int[] nums) {
    int four =0;
    int two= 0;
    for(int i=0; i0 && two==0) {
    return true;
    }

    else if(two>0 && four==0) {
    return true;
    }

    else{
    return false;
    }
    }

    ReplyDelete
  13. public boolean either24(int[] nums) {
    if (Arrays.toString(nums).contains("2, 2")) {
    if (Arrays.toString(nums).contains("4, 4")) {
    return false;
    }
    }
    return Arrays.toString(nums).contains("2, 2")
    || Arrays.toString(nums).contains("4, 4");
    }

    ReplyDelete
  14. public boolean either24(int[] nums) {
    return Arrays.toString(nums).contains("2, 2") != Arrays.toString(nums).contains("4, 4");
    }

    ReplyDelete
  15. public boolean either24(int[] nums) {
    return Arrays.toString(nums).contains("2, 2") != Arrays.toString(nums).contains("4, 4");
    }

    ReplyDelete
  16. public boolean either24(int[] nums) {
    boolean is2 = false;
    boolean is4 = false;

    for(int i = 0; i<nums.length-1;i++){
    if(nums[i]==2 && nums[i+1]==2){
    is2 = true;
    }
    else if(nums[i]==4 && nums[i+1]==4){
    is4 = true;
    }
    }

    if(is2 && is4){
    return false;
    }else if( !is2 & !is4){
    return false;
    }else {
    return true;
    }

    }

    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