Solved

tripleUp challenge

Posted on 2016-08-08
7
104 Views
Last Modified: 2016-08-09
Hi,

I am working on below challenge

http://codingbat.com/prob/p137874

Psedo code description of logical approach :
1. Loop throguh given array
2. check if array i th element and adjacent one and then adjacent ones all differ by 1 from before one.
3. if yes return true
4. if no return false

I wrote my code as below


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

Open in new window






I am passing all tests

Expected      Run            
tripleUp([1, 4, 5, 6, 2]) → true      true      OK      
tripleUp([1, 2, 3]) → true      true      OK      
tripleUp([1, 2, 4]) → false      false      OK      
tripleUp([1, 2, 4, 5, 7, 6, 5, 6, 7, 6]) → true      true      OK      
tripleUp([1, 2, 4, 5, 7, 6, 5, 7, 7, 6]) → false      false      OK      
tripleUp([1, 2]) → false      false      OK      
tripleUp([1]) → false      false      OK      
tripleUp([]) → false      false      OK      
tripleUp([10, 9, 8, -100, -99, -98, 100]) → true      true      OK      
tripleUp([10, 9, 8, -100, -99, 99, 100]) → false      false      OK      
tripleUp([-100, -99, -99, 100, 101, 102]) → true      true      OK      
tripleUp([2, 3, 5, 6, 8, 9, 2, 3]) → false      false      OK      
other tests
OK      

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
7 Comments
 
LVL 21

Accepted Solution

by:
Amitkumar Panchal earned 250 total points
ID: 41749096
Its a simple program, cant make more changes to it. However, made few changes that will improve the performance (can be visible when elements are more).

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

Open in new window

0
 
LVL 27

Assisted Solution

by:BigRat
BigRat earned 125 total points
ID: 41749110
If the first entry plus 1 is going to be equal to the second entry, then the second entry is equal to the first entry (index 0) plus 1 (index 1) and so each entry is going to be equal to the first entry plus the index (starting from zero).  Consequently you only have to check that each entry is equal to entry 0 plus the index. Thus you only test each entry once, instead of two or more times and you can start the loop at entry 1 instead of zero.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41749585
Its a simple program, cant make more changes to it. However, made few changes that will improve the performance (can be visible when elements are more).

difference from my code is removal of else block right.
How it impact performance?

If the first entry plus 1 is going to be equal to the second entry, then the second entry is equal to the first entry (index 0) plus 1 (index 1) and so each entry is going to be equal to the first entry plus the index (starting from zero).  Consequently you only have to check that each entry is equal to entry 0 plus the index. Thus you only test each entry once, instead of two or more times and you can start the loop at entry 1 instead of zero.

if my test case  {1,4,5,6,2}
first entry means 1?
0
Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

 
LVL 36

Assisted Solution

by:mccarl
mccarl earned 125 total points
ID: 41749674
difference from my code is removal of else block right.
How it impact performance?

The difference is the removal of else block AND returning true straight away (at line 5) rather than waiting until the end of the loop to return the result. And it is this last point that could improve performance. Imagine if the array had 1 million entries and the first 3 entries were 1, 2, 3. In your code, you have scanned through all 1 million records before you return a result, whereas Amit's code would return the result after the first loop iteration.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41749729
Imagine if the array had 1 million entries and the first 3 entries were 1, 2, 3. In your code, you have scanned through all 1 million records before you return a result, whereas Amit's code would return the result after the first loop iteration.

even though my else block empty code scan all 1 million records before returning result. I got it
0
 
LVL 36

Expert Comment

by:mccarl
ID: 41749777
even though my else block empty code scan all 1 million records before returning result. I got it
Yes, but just to be clear, it's nothing really to do with the else block. It is because your code only has a return statement right at the end of the method, ie. the loop must fully complete before your code returns anything.

Whereas, Amit's code has a return statement inside the loop and so it has the possibility of returning from the method BEFORE the loop fully completes.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41749779
Yes, but just to be clear, it's nothing really to do with the else block. It is because your code only has a return statement right at the end of the method, ie. the loop must fully complete before your code returns anything.

Whereas, Amit's code has a return statement inside the loop and so it has the possibility of returning from the method BEFORE the loop fully completes.

makes now perfect sense
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

691 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