Java > Array-2 > matchUp (CodingBat Solution)

Problem:

Given arrays nums1 and nums2 of the same length, for every element in nums1, consider the corresponding element in nums2 (at the same index). Return the count of the number of times that the two elements differ by 2 or less, but are not equal.

matchUp({1, 2, 3}, {2, 3, 10}) → 2
matchUp({1, 2, 3}, {2, 3, 5}) → 3
matchUp({1, 2, 3}, {2, 3, 3}) → 2


Solution:

public int matchUp(int[] nums1, int[] nums2) {
  int count =0;
  for (int i =0; i <nums1.length ;i++)
  if (Math.abs(nums1[i] - nums2[i]) == 1 || 
               Math.abs(nums1[i] -nums2[i]) == 2 )
  count++;
  return count;
  
}

3 comments:

  1. public int matchUp(int[] nums1, int[] nums2) {
    int count = 0;

    for(int i = 0; i < nums1.length; i++){
    if( (Math.abs(nums1[i] - nums2[i] ) <= 2) && nums1[i] != nums2[i] ){
    count ++;
    }
    }
    return count;
    }

    ReplyDelete
  2. int listA = 0;

    for(int i = 0; i < nums1.length; i++)
    {
    if(Math.abs(nums1[i] - nums2[i]) <= 2 && nums1[i] != nums2[i]) listA += 1;
    }

    return listA;
    }

    ReplyDelete
  3. public int matchUp(int[] nums1, int[] nums2) {
    int sum=0;
    for(int i=0;i0)
    sum++;
    }

    return sum;
    }

    ReplyDelete