Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# fount 9's in java challenge

Posted on 2014-11-05
Medium Priority
247 Views
Hi,

I am working on below challenge.

I wrote as below
``````public class Test26 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] nums={1,9,1,9,4};
System.out.println("return value boolean is--->"+arrayFront9(nums));

}
public static boolean arrayFront9(int[] nums) {
int count=0;
for(int num: nums){
if(num==9)
count++;
}

if(count>=4)
return true;

/*if(count<4)
return false;*/
return false;
}

}
``````

I am failing in one test case as below

Expected      Run
arrayFront9({1, 2, 9, 3, 4}) → true      true      OK
arrayFront9({1, 2, 3, 4, 9}) → false      true      X
arrayFront9({1, 2, 3, 4, 5}) → false      false      OK
arrayFront9({9, 2, 3}) → true      true      OK
arrayFront9({1, 9, 9}) → true      true      OK
arrayFront9({1, 2, 3}) → false      false      OK
arrayFront9({1, 9}) → true      true      OK
arrayFront9({5, 5}) → false      false      OK
arrayFront9({2}) → false      false      OK
arrayFront9({9}) → true      true      OK
arrayFront9({}) → false      false      OK
arrayFront9({3, 9, 2, 3, 3}) → true      true      OK
Correct for more than half the tests

0
Question by:gudii9
• 9
• 7
• 5

LVL 28

Expert Comment

ID: 40425004
Your code is counting the number of 9s.

The problem is to see if there is a 9 in the first 4 values.

Can you see how I changed your method and why this works?

``````	public boolean arrayFront9(int[] nums) {
int count=0;
for(int num: nums){
count++ ;

if(num==9 && count <= 4)
return true ;
}

return false;
}
``````
0

LVL 7

Author Comment

ID: 40425023
count++ ;

this counts number of elements in nums array right?

if(num==9 && count <= 4
above statement it will make sure it will check till 4th position only not beyond that and also value is 9 right?
0

LVL 28

Expert Comment

ID: 40425040
Yep that's all correct.
0

LVL 85

Expert Comment

ID: 40425570
Can you post a link to this challenge?
0

LVL 28

Expert Comment

ID: 40425574
It's this one:
http://codingbat.com/prob/p186031

Doug
0

LVL 85

Expert Comment

ID: 40425652
The method of http:#a40425004 does work for that, but it may be worth noting that if you have a very long array, it could do a lot more work than it needs to before returning false.

I also notice that the code in the original question fails in more than just the one case.
0

LVL 7

Author Comment

ID: 40426193
The method of http:#a40425004 does work for that, but it may be worth noting that if you have a very long array, it could do a lot more work than it needs to before returning false.

The method of http:#a40425004 does work for that, but it may be worth noting that if you have a very long array, it could do a lot more work than it needs to before returning false.

correct it fails for all arrays having 9's having bigger count than  4
0

LVL 28

Accepted Solution

dpearson earned 1000 total points
ID: 40426848

The version above loops through all members of the array.

But since we only have to check the first 4 items we could do this instead:

``````	public boolean arrayFront9(int[] nums) {
for (int count = 0 ; count < nums.length && count < 4 ; count++) {
int num = nums[count];
if(num==9)
return true ;
}

return false;
}
``````

This is an example of switching between the two different types of loops (for int i = 0 ; i ... ) and (for num : nums), so we can check the count of items so far.

Can you see why it makes sense here to use the older style (for int i = 0 ; i < ... ; i++) style of loop here?

Doug
0

LVL 85

Assisted Solution

ozo earned 1000 total points
ID: 40426867
Is there is a way to improve it?
You might change
if(num==9 && count <= 4)
return true ;
to
if(num==9 )
return true ;
if( count==4 )
return false;
0

LVL 7

Author Comment

ID: 40426916
count < nums.length && count < 4

I see 2 separate conditions clubbed together with &&.

How many number of conditions we can club together?

I was thinking we should say instead as below
count < nums.length && count < =4
0

LVL 7

Author Comment

ID: 40426921
``````You might change
if(num==9 && count <= 4)
return true ;
to
if(num==9 )
return true ;
if( count==4 )
return false;
``````

we are still checking all the elements in for each loop and do above step right. How it is going to be different. please advise
0

LVL 28

Expert Comment

ID: 40426955
count < nums.length && count < =4

If you start the loop with "count = 0" you want "count < 4" to do 4 loops.
If you start the loop with "count = 1" you want "count <= 4" to do 4 loops.

We usually loop with counts that start from 0 in the software business, because arrays start from index 0, so you can just do:
int num = nums[count] ;
if the count starts with 0.

You might change
if(num==9 && count <= 4)
return true ;
to
if(num==9 )
return true ;
if( count==4 )
return false;

Yes that's another way to do it.

Doug
0

LVL 7

Author Comment

ID: 40427058
``````You might change
if(num==9 && count <= 4)
return true ;
to
if(num==9 )
return true ;
if( count==4 )
return false;
``````
do you mean it should be like below(within foreach loop?  Please advise

public boolean arrayFront9(int[] nums) {
int count=0;
for(int num: nums){
count++ ;

if(num==9 )
return true ;
if( count==4 )
return false;
}

return false;
}
0

LVL 28

Expert Comment

ID: 40427328
Yes that's what ozo is suggesting.

Doug
0

LVL 7

Author Comment

ID: 40428379
what would be the difference. To  me both approaches looks same.please advise.
0

LVL 85

Expert Comment

ID: 40428429
The difference between http:#a40426848 and http:#a40427058 would be mainly which style of for loop you prefer.
In this case, since you do need the count index, and don't always scan all elements of the array, I might be more inclined to use http:#a40426848, but other preferences can be equally valid.
0

LVL 7

Author Comment

ID: 40428618
In this case, since you do need the count index, and don't always scan all elements of the array
which case you are referring http:#a40426848  or http:#a40427058

in http:#a40427058 scanning of all elements happening in the below lines right?

for(int num: nums){
count++ ;
as given in the complete code below

``````public boolean arrayFront9(int[] nums) {
int count=0;
for(int num: nums){
count++ ;

if(num==9 )
return true ;
if( count==4 )
return false;
}

return false;
}
``````
0

LVL 85

Expert Comment

ID: 40428669
The case I am referring to is the problem presented by http://codingbat.com/prob/p186031

In  http:#a40427058, scanning of all elements is limited by
if( count==4 )
return false;
0

LVL 28

Expert Comment

ID: 40428945
what would be the difference. To  me both approaches looks same.please advise.

They're both logically equivalent since they both solve the problem.

So in that case you generally want the simpler solution, which is usually the one using less lines of code (although not always).  So in this situation I'd prefer:

``````	public boolean arrayFront9(int[] nums) {
for (int count = 0 ; count < nums.length && count < 4 ; count++) {
int num = nums[count];
if(num==9)
return true ;
}

return false;
}
``````

It also reads a little easier - because this line:

for (int count = 0 ; count < nums.length && count < 4 ; count++) {

explicitly indicates that we're looping through at most 4 elements of the array, which is otherwise something you only discover later while reading the method (in the alternative solutions, this test for count == 4 comes later).

But at this point it's mostly about a sense of whichever you find clearer.  There are no hard and fast rules and really to be a good developer the key is seeing that there are different ways to solve any given problem - so you can weigh the pros and cons of each option.

Doug
0

LVL 7

Author Comment

ID: 40432804
``````	public boolean arrayFront9(int[] nums) {
for (int count = 0 ; count < nums.length && count < 4 ; count++) {
int num = nums[count];
if(num==9)
return true ;
}

return false;
}
``````

I too feel above code is more readable and simple.Rather than checking inside for loop by putting separate if condition to see if count==4 as below

if(num==9 )
return true ;
if( count==4 )
return false;
0

LVL 7

Author Comment

ID: 40432846
Can you see why it makes sense here to use the older style (for int i = 0 ; i < ... ; i++) style of loop here?

i finally got using old for loop rather than new for each loop is advisable in this case to avoid scanning elements beyond count 4 right. please advise
0

## Featured Post

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.
In this post we will learn different types of Android Layout and some basics of an Android App.
This video teaches viewers about errors in exception handling.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
###### Suggested Courses
Course of the Month9 days, 19 hours left to enroll