# Start1 challenge

Hi,

I am working on below challenge

http://codingbat.com/prob/p109660

i wrote as below and failing some tests
``````public int start1(int[] a, int[] b) {
int num=0;

for(int i=0;i<a.length;i++){
if(a[0]==1)
num++;
}

for(int i=0;i<b.length;i++){
if(b[0]==1)
num++;
}
return num;

}
``````

``````Expected	Run
start1({1, 2, 3}, {1, 3}) → 2	5	X
start1({7, 2, 3}, {1}) → 1	1	OK
start1({1, 2}, {}) → 1	2	X
start1({}, {1, 2}) → 1	2	X
start1({7}, {}) → 0	0	OK
start1({7}, {1}) → 1	1	OK
start1({1}, {1}) → 2	2	OK
start1({7}, {8}) → 0	0	OK
start1({}, {}) → 0	0	OK
start1({1, 3}, {1}) → 2	3	X
other tests
X
``````

how to improve and fix the code. please advise
LVL 7
Asked:
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
return (a.length>0&&a[0]==1?1:0)+(b.length>0&&b[0]==1?1:0);
0
Commented:
How many times do you increment num?
0
Commented:
Why do you iterate?
0
Commented:
``````public int start1(int[] a, int[] b) {

boolean avalid = a.length>0;
boolean bvalid = b.length>0;

if(avalid&bvalid){return a[0]+b[0]==2?2:a[0]==1?1:b[0]==1?1:0;}

if(avalid) {return a[0]==1?1:0;}

if(bvalid) {return b[0]==1?1:0;}

return 0;

}
``````
0
Commented:
a[0]+b[0]==2 depends on assumptions about the element values which are not guaranteed by the problem specification, even if they happen to be satisfied by the test examples.
0
Commented:
depends on assumptions about the element values which are not guaranteed by the problem specification,

which might be true under any other condition apart from this one, where there are ONLY 2 arrays.
0
Commented:
"Start with 2 int arrays" is guaranteed by the problem specification.
(on the other hand, a[0]>=1 is not guaranteed by the problem specification)
0
Commented:
This is the problem spec :

>>Start with 2 int arrays, a and b, of any length. Return how many of the arrays have 1 as their first element. <<

I don't think you have followed the logic in my code -  which does NOT include this statement of yours :
``````(on the other hand, a[0]>=1
``````
0
Commented:
i don't think you have followed the logic in my comments, which do not claim that your code includes "a[0]>=1",
rather they claim that the problem spec does not include "a[0]>=1"
and that your code does include "a[0]+b[0]==2"
0
Commented:
None of the test arrays start with "0".
0
Commented:
And I do not claim that any of the test arrays start with "0", nor do I claim that any of them start with "-5",
In fact, I explicitly said "even if they happen to be satisfied by the test examples".
0
Commented:
So given that one of the arrays could start with 0, but the other with 2, or one array holds a complementary negative value, then yes, the test would fail.
0
Commented:
The problem - or one of them - with this Codingbat's site tests, is that the datatypes - like our ints here - are never constrained to a "customised" range, yet neither are they qualified as being within the range of Java int, nevertheless the "other tests" that the site subjects the code to are not exposed either. That means that it becomes nearly impossible to tell what sort of a challenge it really is - one can only assume that the "other tests" exclude 0 values in arrays, as well as negative values. This isn't really very good for a full appreciation of where code can fall over in the real world, because the test data is incomplete and therefore meaningless.
0
Author Commented:
``````public int start1(int[] a, int[] b) {
int num=0;

//if(a.length>=1 & b.length>=1)
//{
if(a[0]==1)
{
num++;
}

if(b[0]==1)
{
num++;
}
return num;

}

//}

//else
//{
//return null;
//}
//}
``````
i tried as above and passed few tests but failing the edge cases. please advise
0
Commented:
``````public int start1(int[] a, int[] b) {
int num=0;

if(a.length>0&&a[0]==1)
{
num++;
}

if(b.length>0&&b[0]==1)
{
num++;
}
return num;

}
``````
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
when should i iterate and when i should not iterate in arrays or in general. Please advise
0
Commented:
In general, iterate when you are doing a lot of the same thing.
0
Author Commented:
lot of the same thing.

same thing means for example if we want to check each and every number of array is equal to even or odd number etc?

any other examples of same thing??

please advise
0
Commented:
I wont be participating in any more of your questions, Asker. (Just in case you wonder why, I don't have time to invest on such vast scales for such little progress).
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.