Solved

modThree challenge

Posted on 2016-08-07
4
76 Views
Last Modified: 2016-08-08
I am working on below challenge

http://codingbat.com/prob/p159979

Psedo code description of approach :
1. Loop throguh given array
2. check if array adjacent three elements are even or odd
3. if yes return true
4. if no return false

I wrote my code as below

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

Open in new window




I am failing couple of  tests
Expected      Run            
modThree([2, 1, 3, 5]) → true      true      OK      
modThree([2, 1, 2, 5]) → false      false      OK      
modThree([2, 4, 2, 5]) → true      true      OK      
modThree([1, 2, 1, 2, 1]) → false      false      OK      
modThree([9, 9, 9]) → true      true      OK      
modThree([1, 2, 1]) → false      false      OK      
modThree([1, 2]) → false      false      OK      
modThree([1]) → false      false      OK      
modThree([]) → false      false      OK      
modThree([9, 7, 2, 9]) → false      false      OK      
modThree([9, 7, 2, 9, 2, 2]) → false      Exception:java.lang.ArrayIndexOutOfBoundsException: 6 (line number:10)      X      
modThree([9, 7, 2, 9, 2, 2, 6]) → true      true      OK
How to improve my design, approach, code? please advise
0
Comment
Question by:gudii9
4 Comments
 
LVL 27

Expert Comment

by:d-glitch
ID: 41746612
There is one else in your pseudo code, but four in you code.
What does that mean?
0
 
LVL 4

Accepted Solution

by:
Hammadh Abdul Rahman earned 250 total points
ID: 41746640
Just remove lines 10-13 and 20-23. Also, the else block is unnecessary.

As the message says, ArrayIndexOutOfBoundsException is occurring at line 10. The same error is possible at line 20 also.

This is because of the reference to nums[i+3]. modThree([9, 7, 2, 9, 2, 2]) would give a array of length 6. Since i<nums.length-2 is the loop condition, max 'i' value would be 3. So nums[3+3] would give error because highest index is 5.
0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 250 total points
ID: 41747460
Again, I might suggest starting at index = 2 and searching backwards (e.g. if i is even and i-1 and i-2 are even or i is odd and i-1 and i-2 are odd) to avoid any invalid index. Note, you would need to check for a minimum array length of 3.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41747599
public boolean modThree(int[] nums) {
  boolean result=false;
  
  for(int i=0;i<nums.length-2;i++){
    if(nums[i]%2==0&&nums[i+1]%2==0&&nums[i+2]%2==0){
      result=true;
      return result;
    }
    
  
     else if(nums[i]%2==1&&nums[i+1]%2==1&&nums[i+2]%2==1){
      result=true;
      return result;
    }
    //return result;
  }
  return result;
}

Open in new window

above passed all tests
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# code editing and collaboration 3 98
Arduino EDI - Programming Language - Voice Recorder 4 66
replace a word with other 1 40
Windows 10 IE Certificate Issue 10 41
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now