Solved

copyEvens challenge

Posted on 2016-08-30
6
116 Views
Last Modified: 2016-09-01
I am working on below challenge
http://codingbat.com/prob/p134174

Psedo code:
0. create new array of given count
1. for given arry loop through it
2. check if each element is even or not
3. if even fill till the new array ends
4. return new array
I wrote my code as below

public int[] copyEvens(int[] nums, int count) {
  int[] arr=new int[count];
  for(int i=0;i<nums.length-1;i++){
    if(nums[i]%2==0){
      arr[i]=nums[i];
      
      
    }
  }
  return arr;
}

Open in new window






I am not passing all tests
xpected      Run            
copyEvens([3, 2, 4, 5, 8], 2) → [2, 4]      Exception:java.lang.ArrayIndexOutOfBoundsException: 2 (line number:5)      X      
copyEvens([3, 2, 4, 5, 8], 3) → [2, 4, 8]      [0, 2, 4]      X      
copyEvens([6, 1, 2, 4, 5, 8], 3) → [6, 2, 4]      Exception:java.lang.ArrayIndexOutOfBoundsException: 3 (line number:5)      X      
copyEvens([6, 1, 2, 4, 5, 8], 4) → [6, 2, 4, 8]      [6, 0, 2, 4]      X      
copyEvens([3, 1, 4, 1, 5], 1) → [4]      Exception:java.lang.ArrayIndexOutOfBoundsException: 2 (line number:5)      X      
copyEvens([2], 1) → [2]      [0]      X      
copyEvens([6, 2, 4, 8], 2) → [6, 2]      Exception:java.lang.ArrayIndexOutOfBoundsException: 2 (line number:5)      X      
copyEvens([6, 2, 4, 8], 3) → [6, 2, 4]      [6, 2, 4]      OK      
copyEvens([6, 2, 4, 8], 4) → [6, 2, 4, 8]      [6, 2, 4, 0]      X      
copyEvens([1, 8, 4], 1) → [8]      Exception:java.lang.ArrayIndexOutOfBoundsException: 1 (line number:5)      X      
copyEvens([1, 8, 4], 2) → [8, 4]      [0, 8]      X      
copyEvens([2, 8, 4], 2) → [2, 8]      [2, 8]      OK      
other tests
X      
Your progress graph for this problem

How to improve/modify my design, code and any other alternate approaches. 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
  • 4
6 Comments
 
LVL 9

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 41777457
In for loop it should be
I < nums.length

And other observation is,  you are passing no of even elements are 2 but in actuality trying to access 3. So getting array out of bound exception
0
 
LVL 9

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 41777460
int temp =0;

for(int i=0;i<nums.length;i++){
      if(nums[i]%2==0 &&  temp < count){
          arr[temp++]=nums[i];
      }
}

But this code can be improved.
0
 
LVL 9

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 41777468
public int[] copyEvens(int[] nums, int count) {
  int[] arr=new int[count];
  int temp = 0;
  for(int i=0; i<nums.length; i++){
    if(nums[i]%2==0 &&  temp < count){ 
          arr[temp++]=nums[i]; 
    }
  }
  return arr;
}

Open in new window

0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 9

Accepted Solution

by:
Subrat (C++ windows/Linux) earned 500 total points
ID: 41777470
Better would be instead of iterating all elements of array, loop only upto your count is reached and exit from loop.

Ex:
public int[] copyEvens(int[] nums, int count) {
  int[] arr=new int[count];
  int temp = 0;
  for(int i=0; (temp < count) && (i < nums.length); ++i){
    if(nums[i]%2==0){ 
          arr[temp++]=nums[i]; 
    }
  }
  return arr;
}

Open in new window

0
 
LVL 3

Expert Comment

by:Donna
ID: 41777472
FYI ... gudii9 has posted about 20 challenges from codingbat.com site...just to watch you guys jump through hoops. Is that what EE does? If it is, I for one, am leaving...
0
 
LVL 7

Author Comment

by:gudii9
ID: 41780956
public int[] copyEvens(int[] nums, int count) {
  int[] arr=new int[count];
  int temp = 0;
  for(int i=0;(temp<count)&&(i<nums.length);i++){
    if(nums[i]%2==0){
      arr[temp++]=nums[i];
      
      
    }
  }
  return arr;
}

Open in new window


i like temp approach. Above (with i++ instead of ++i) also passed all tests. Any difference using i++ or ++i in this challenge?
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
This is about my first experience with programming Arduino.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

726 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