count instances where the second "6" is actually a 7.
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;
}
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.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.if (nums[i]==nums[i+1] ... )
, you don't really want 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;
}
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
return the number of times that two 6's are next to each other
if(nums[i]==6&&nums[i+1]==7)
if(nums[i]==6 && (nums[i+1] == 6 || nums[i+1] == 7))
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;
}
Title | # Comments | Views | Activity |
---|---|---|---|
Passing list of object to Oracle Database Procedure | 3 | 42 | |
pairs challenge | 5 | 44 | |
bitbucket vs gitbucket | 3 | 32 | |
Java array passed to SQL where clause | 23 | 43 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
23 Experts available now in Live!