asked on

# second 6 is 7 challenge

Hi,

i am  trying below challenge

http://codingbat.com/prob/p110019

I have not understood on problem statement when they say like
count instances where the second "6" is actually a 7.

i wrote as below

``````public int array667(int[] nums) {
int count=0;
for(int i=0;i<nums.length-1;i++)
{
if(nums[i]==nums[i+1])
count++;
}
return count;
}``````

some test cases are failing as below
Expected      Run
array667({6, 6, 2}) → 1      1      OK
array667({6, 6, 2, 6}) → 1      1      OK
array667({6, 7, 2, 6}) → 1      0      X
array667({6, 6, 2, 6, 7}) → 2      1      X
array667({1, 6, 3}) → 0      0      OK
array667({6, 1}) → 0      0      OK
array667({}) → 0      0      OK
array667({3, 6, 7, 6}) → 1      0      X
array667({3, 6, 6, 7}) → 2      1      X
array667({6, 3, 6, 6}) → 1      1      OK
array667({6, 7, 6, 6}) → 2      1      X
array667({1, 2, 3, 5, 6}) → 0      0      OK
array667({1, 2, 3, 6, 6}) → 1      1      OK

please advise
SOLUTION
ozo

membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
dpearson

Your code detects whenever there are 2 identical numbers appearing one after another in the array.

So your code would detect
6,6   (which is correct)
but also
3,3  (which is not)

Also the instructions say you need to find pairs like this:
6,6
or
6,7

So you want code something like this:
``````public int array667(int[] nums) {
int count=0;
for(int i=0;i<nums.length-1;i++)
{
if(nums[i]==6 && nums[i+1] == 7)
count++;
}
return count;
}``````
which would detect one of those pairs - the 6,7 combination.

So you need to add code to make it also recognize the 6,6 combination.

ASKER

``````public int array667(int[] nums) {
int count=0;
for(int i=0;i<nums.length-1;i++)
{
if(nums[i]==nums[i+1]|| nums[i]==6&&nums[i+1]==7)
count++;
}
return count;
}``````
i wrote as above . It pass all the tests.
does it looks ok?
Although it passes all the tests at the challenge link, the count would be different from the solution given at the challenge link if it was tested with
array667({1,1})
Yeah - like ozo pointed out, you don't really want this

``if (nums[i]==nums[i+1] ... )``

in the test as that's just checking if two numbers in the sequence are identical.

The problem is asking for specifically checking for 6s and 7s, not any two numbers.

Doug

ASKER

, you don't really want this

By removing that as below

``````public int array667(int[] nums) {
int count=0;
for(int i=0;i<nums.length-1;i++)
{
if(nums[i]==6&&nums[i+1]==7)
count++;
}
return count;
}``````

i am failing test cases as below
``````Expected	Run
array667({6, 6, 2}) → 1	0	X
array667({6, 6, 2, 6}) → 1	0	X
array667({6, 7, 2, 6}) → 1	1	OK
array667({6, 6, 2, 6, 7}) → 2	1	X
array667({1, 6, 3}) → 0	0	OK
array667({6, 1}) → 0	0	OK
array667({}) → 0	0	OK
array667({3, 6, 7, 6}) → 1	1	OK
array667({3, 6, 6, 7}) → 2	1	X
array667({6, 3, 6, 6}) → 1	0	X
array667({6, 7, 6, 6}) → 2	1	X
array667({1, 2, 3, 5, 6}) → 0	0	OK
array667({1, 2, 3, 6, 6}) → 1	0	X	    ``````

Please advise

ASKER

return the number of times that two 6's are next to each other

how to incorporate above in to if condition . please advise
Can you think of a condition that is true when two 6's are next to each other?

ASKER

`` if(nums[i]==6&&nums[i+1]==7)``

as of now i am able to check 6 and 7 are adjacent. Since i am using 6 in this condition not sure how to check if another 6 also adjacent. Not able to think. Also i wonder why thinking is hard sometimes. please advise
Perhaps after trying http://codingbat.com/prob/p121853
you can then come back to http://codingbat.com/prob/p110019 with a new idea.
ASKER CERTIFIED SOLUTION

membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.

ASKER

``````public int array667(int[] nums) {
int count=0;
for(int i=0;i<nums.length-1;i++)
{
if(nums[i]==6 && (nums[i+1] == 6 || nums[i+1] == 7))
count++;
}
return count;
}``````

i like above solution where we can check same index for 2 different values 6 as well as 7. This is new perspective opened for me.
This is new perspective opened for me.
Excellent.  That's the whole point of expert's exchange :)

Glad you've got this now.

Doug