Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

matchUp  challenge

Posted on 2016-08-03
6
Medium Priority
?
116 Views
Last Modified: 2016-08-04
Hi,

I am working on below challenge
http://codingbat.com/prob/p136254

Psedo code description of approach :
1. loop through both arrays
2. identify each index element and compare to see differnce is atmost 2
3. if step2 is true return true
4. otherwise return false

I wrote my code as below

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

Open in new window




I am not passing all tests due to edge cases.
Expected      Run            
matchUp([1, 2, 3], [2, 3, 10]) → 2      1      X      
matchUp([1, 2, 3], [2, 3, 5]) → 3      1      X      
matchUp([1, 2, 3], [2, 3, 3]) → 2      2      OK      
matchUp([5, 3], [5, 5]) → 1      1      OK      
matchUp([5, 3], [4, 4]) → 2      1      X      
matchUp([5, 3], [3, 3]) → 1      1      OK      
matchUp([5, 3], [2, 2]) → 1      0      X      
matchUp([5, 3], [1, 1]) → 1      0      X      
matchUp([5, 3], [0, 0]) → 0      0      OK      
matchUp([4], [4]) → 0      0      OK      
matchUp([4], [5]) → 1      0      X      
other tests
X      

How to improve my design, approach, code? please advise
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 16

Accepted Solution

by:
krakatoa earned 2000 total points
ID: 41742228
WHY are you comparing nums1 indexes with nums2 indexes+1 ????????

You haven't read the question properly again.
0
 
LVL 7

Author Comment

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

Open in new window

oops that was type. I fixed as above. still failing some tests. please advise
Expected      Run            
matchUp([1, 2, 3], [2, 3, 10]) → 2      2      OK      
matchUp([1, 2, 3], [2, 3, 5]) → 3      2      X      
matchUp([1, 2, 3], [2, 3, 3]) → 2      2      OK      
matchUp([5, 3], [5, 5]) → 1      1      OK      
matchUp([5, 3], [4, 4]) → 2      1      X      
matchUp([5, 3], [3, 3]) → 1      1      OK      
matchUp([5, 3], [2, 2]) → 1      0      X      
matchUp([5, 3], [1, 1]) → 1      0      X      
matchUp([5, 3], [0, 0]) → 0      0      OK      
matchUp([4], [4]) → 0      0      OK      
matchUp([4], [5]) → 1      0      X      
other tests
X      
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41742495
please advise

My advice is that you read the question properly . . .  which you still haven't done.

You are being lazy again, and expecting us to tell you what to think, despite the fact that we've already told you HOW to think.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 7

Author Comment

by:gudii9
ID: 41742752
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)||(Math.abs(nums1[i]-nums2[i])==1)){
      count++;
    }
  }
  return count;
}

Open in new window

Expected      Run            
matchUp([1, 2, 3], [2, 3, 10]) → 2      2      OK      
matchUp([1, 2, 3], [2, 3, 5]) → 3      3      OK      
matchUp([1, 2, 3], [2, 3, 3]) → 2      2      OK      
matchUp([5, 3], [5, 5]) → 1      1      OK      
matchUp([5, 3], [4, 4]) → 2      2      OK      
matchUp([5, 3], [3, 3]) → 1      1      OK      
matchUp([5, 3], [2, 2]) → 1      1      OK      
matchUp([5, 3], [1, 1]) → 1      1      OK      
matchUp([5, 3], [0, 0]) → 0      0      OK      
matchUp([4], [4]) → 0      0      OK      
matchUp([4], [5]) → 1      1      OK      
other tests
OK      

i made mistake. Did not notice two or less but not equal. I fixed as above now passes all tests. Any suggestion to improve above code?
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41742916
No. You've done it this time.

I'd suggest however that you take the biggest part of the lesson here from the (eventually correct) interpretation of the question. That's always going to be the challenge.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41743199
No. You've done it this time.

I'd suggest however that you take the biggest part of the lesson here from the (eventually correct) interpretation of the question.

English is not my first language. some of the sentences i am having hard time to properly interpret. I will definitely do my best in improving.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question