java Codingbat notAlone — why doesn't it work for this specific example
We'll say that an element in an array is "alone" if there are values before and after it, and those values are different from it. Return a version of the given array where every instance of the given value which is alone is replaced by whichever value to its left or right is larger.
notAlone([1, 2, 3], 2) → [1, 3, 3]
notAlone([1, 2, 3, 2, 5, 2], 2) → [1, 3, 3, 5, 5, 2]
notAlone([3, 4], 3) → [3, 4]
public int notAlone(int nums, int val) {
for(int k = 1 ; k<nums.length; k++)
{
if(k!= nums.length-1)
{
int max = nums[k];
if(nums[k-1]>nums[k])
max = nums[k-1];
else if(nums[k+1] > nums[k])
max = nums[k+1];
if(nums[k-1] != nums[k] && nums[k] != nums[k+1])
nums[k] = max;
}
}
return nums;
}
When I ran this on codingbat, it worked for all examples except for this:
notAlone([1, 2, 3, 2, 5, 2], 2) should return[1, 3, 3, 5, 5, 2], but instead mine returned[1, 3, 3, 3, 5, 2].
I am really stuck on how to solve this, because in my mind, what I've written should work for this specific example as well, but apparently it doesn't. Where does my error come from? How should I re-write my code? Any help would really be appreciated!
java arrays loops for-loop if-statement
add a comment |
We'll say that an element in an array is "alone" if there are values before and after it, and those values are different from it. Return a version of the given array where every instance of the given value which is alone is replaced by whichever value to its left or right is larger.
notAlone([1, 2, 3], 2) → [1, 3, 3]
notAlone([1, 2, 3, 2, 5, 2], 2) → [1, 3, 3, 5, 5, 2]
notAlone([3, 4], 3) → [3, 4]
public int notAlone(int nums, int val) {
for(int k = 1 ; k<nums.length; k++)
{
if(k!= nums.length-1)
{
int max = nums[k];
if(nums[k-1]>nums[k])
max = nums[k-1];
else if(nums[k+1] > nums[k])
max = nums[k+1];
if(nums[k-1] != nums[k] && nums[k] != nums[k+1])
nums[k] = max;
}
}
return nums;
}
When I ran this on codingbat, it worked for all examples except for this:
notAlone([1, 2, 3, 2, 5, 2], 2) should return[1, 3, 3, 5, 5, 2], but instead mine returned[1, 3, 3, 3, 5, 2].
I am really stuck on how to solve this, because in my mind, what I've written should work for this specific example as well, but apparently it doesn't. Where does my error come from? How should I re-write my code? Any help would really be appreciated!
java arrays loops for-loop if-statement
nums[k+1] > nums[k]
andnums[k-1]>nums[k]
. You are assumingnums[k]
is currently the largest number. Change them tonums[k+1]>max
andnums[k-1]>max
accordingly.
– Andrew Tobilko
Nov 24 '18 at 16:03
You wroteif-else-if
assuming that ifnums[k-1]>max
istrue
, thennums[k+1]>max
can't betrue
. Removeelse
, change it to twoif
s.
– Andrew Tobilko
Nov 24 '18 at 16:04
add a comment |
We'll say that an element in an array is "alone" if there are values before and after it, and those values are different from it. Return a version of the given array where every instance of the given value which is alone is replaced by whichever value to its left or right is larger.
notAlone([1, 2, 3], 2) → [1, 3, 3]
notAlone([1, 2, 3, 2, 5, 2], 2) → [1, 3, 3, 5, 5, 2]
notAlone([3, 4], 3) → [3, 4]
public int notAlone(int nums, int val) {
for(int k = 1 ; k<nums.length; k++)
{
if(k!= nums.length-1)
{
int max = nums[k];
if(nums[k-1]>nums[k])
max = nums[k-1];
else if(nums[k+1] > nums[k])
max = nums[k+1];
if(nums[k-1] != nums[k] && nums[k] != nums[k+1])
nums[k] = max;
}
}
return nums;
}
When I ran this on codingbat, it worked for all examples except for this:
notAlone([1, 2, 3, 2, 5, 2], 2) should return[1, 3, 3, 5, 5, 2], but instead mine returned[1, 3, 3, 3, 5, 2].
I am really stuck on how to solve this, because in my mind, what I've written should work for this specific example as well, but apparently it doesn't. Where does my error come from? How should I re-write my code? Any help would really be appreciated!
java arrays loops for-loop if-statement
We'll say that an element in an array is "alone" if there are values before and after it, and those values are different from it. Return a version of the given array where every instance of the given value which is alone is replaced by whichever value to its left or right is larger.
notAlone([1, 2, 3], 2) → [1, 3, 3]
notAlone([1, 2, 3, 2, 5, 2], 2) → [1, 3, 3, 5, 5, 2]
notAlone([3, 4], 3) → [3, 4]
public int notAlone(int nums, int val) {
for(int k = 1 ; k<nums.length; k++)
{
if(k!= nums.length-1)
{
int max = nums[k];
if(nums[k-1]>nums[k])
max = nums[k-1];
else if(nums[k+1] > nums[k])
max = nums[k+1];
if(nums[k-1] != nums[k] && nums[k] != nums[k+1])
nums[k] = max;
}
}
return nums;
}
When I ran this on codingbat, it worked for all examples except for this:
notAlone([1, 2, 3, 2, 5, 2], 2) should return[1, 3, 3, 5, 5, 2], but instead mine returned[1, 3, 3, 3, 5, 2].
I am really stuck on how to solve this, because in my mind, what I've written should work for this specific example as well, but apparently it doesn't. Where does my error come from? How should I re-write my code? Any help would really be appreciated!
java arrays loops for-loop if-statement
java arrays loops for-loop if-statement
edited Nov 24 '18 at 15:45
Ivar
2,756113040
2,756113040
asked Nov 24 '18 at 15:43
herminnyherminny
282
282
nums[k+1] > nums[k]
andnums[k-1]>nums[k]
. You are assumingnums[k]
is currently the largest number. Change them tonums[k+1]>max
andnums[k-1]>max
accordingly.
– Andrew Tobilko
Nov 24 '18 at 16:03
You wroteif-else-if
assuming that ifnums[k-1]>max
istrue
, thennums[k+1]>max
can't betrue
. Removeelse
, change it to twoif
s.
– Andrew Tobilko
Nov 24 '18 at 16:04
add a comment |
nums[k+1] > nums[k]
andnums[k-1]>nums[k]
. You are assumingnums[k]
is currently the largest number. Change them tonums[k+1]>max
andnums[k-1]>max
accordingly.
– Andrew Tobilko
Nov 24 '18 at 16:03
You wroteif-else-if
assuming that ifnums[k-1]>max
istrue
, thennums[k+1]>max
can't betrue
. Removeelse
, change it to twoif
s.
– Andrew Tobilko
Nov 24 '18 at 16:04
nums[k+1] > nums[k]
and nums[k-1]>nums[k]
. You are assuming nums[k]
is currently the largest number. Change them to nums[k+1]>max
and nums[k-1]>max
accordingly.– Andrew Tobilko
Nov 24 '18 at 16:03
nums[k+1] > nums[k]
and nums[k-1]>nums[k]
. You are assuming nums[k]
is currently the largest number. Change them to nums[k+1]>max
and nums[k-1]>max
accordingly.– Andrew Tobilko
Nov 24 '18 at 16:03
You wrote
if-else-if
assuming that if nums[k-1]>max
is true
, then nums[k+1]>max
can't be true
. Remove else
, change it to two if
s.– Andrew Tobilko
Nov 24 '18 at 16:04
You wrote
if-else-if
assuming that if nums[k-1]>max
is true
, then nums[k+1]>max
can't be true
. Remove else
, change it to two if
s.– Andrew Tobilko
Nov 24 '18 at 16:04
add a comment |
1 Answer
1
active
oldest
votes
You're over complicating it. You only need to find the max
of the previous and next elements if the current element should be replaced:
public static int notAlone(int nums, int val) {
for(int k = 1 ; k<nums.length - 1; k++)
{
if(nums[k]==val && nums[k-1] != nums[k] && nums[k] != nums[k+1])
nums[k] = Math.max (nums[k-1], nums[k+1]);
}
return nums;
}
BTW, in your solution you ignore the given value (val
):
Return a version of the given array where every instance of the given value which is alone is replaced...
That's not the reason why your code failed in the given case, though. You simply didn't find the correct maximum:
When k==3
:
int max = nums[k]; // max = 2
if(nums[k-1]>nums[k]) // 3 > 2
max = nums[k-1]; // max = 3
else if(nums[k+1] > nums[k]) // no evaluated. therefore you change num[3] to 3 instead of to 5
max = nums[k+1];
If you replaced these 5 lines with:
int max = nums[k-1];
if(nums[k+1] > max)
max = nums[k+1];
you would have gotten the correct output.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459768%2fjava-codingbat-notalone-why-doesnt-it-work-for-this-specific-example%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You're over complicating it. You only need to find the max
of the previous and next elements if the current element should be replaced:
public static int notAlone(int nums, int val) {
for(int k = 1 ; k<nums.length - 1; k++)
{
if(nums[k]==val && nums[k-1] != nums[k] && nums[k] != nums[k+1])
nums[k] = Math.max (nums[k-1], nums[k+1]);
}
return nums;
}
BTW, in your solution you ignore the given value (val
):
Return a version of the given array where every instance of the given value which is alone is replaced...
That's not the reason why your code failed in the given case, though. You simply didn't find the correct maximum:
When k==3
:
int max = nums[k]; // max = 2
if(nums[k-1]>nums[k]) // 3 > 2
max = nums[k-1]; // max = 3
else if(nums[k+1] > nums[k]) // no evaluated. therefore you change num[3] to 3 instead of to 5
max = nums[k+1];
If you replaced these 5 lines with:
int max = nums[k-1];
if(nums[k+1] > max)
max = nums[k+1];
you would have gotten the correct output.
add a comment |
You're over complicating it. You only need to find the max
of the previous and next elements if the current element should be replaced:
public static int notAlone(int nums, int val) {
for(int k = 1 ; k<nums.length - 1; k++)
{
if(nums[k]==val && nums[k-1] != nums[k] && nums[k] != nums[k+1])
nums[k] = Math.max (nums[k-1], nums[k+1]);
}
return nums;
}
BTW, in your solution you ignore the given value (val
):
Return a version of the given array where every instance of the given value which is alone is replaced...
That's not the reason why your code failed in the given case, though. You simply didn't find the correct maximum:
When k==3
:
int max = nums[k]; // max = 2
if(nums[k-1]>nums[k]) // 3 > 2
max = nums[k-1]; // max = 3
else if(nums[k+1] > nums[k]) // no evaluated. therefore you change num[3] to 3 instead of to 5
max = nums[k+1];
If you replaced these 5 lines with:
int max = nums[k-1];
if(nums[k+1] > max)
max = nums[k+1];
you would have gotten the correct output.
add a comment |
You're over complicating it. You only need to find the max
of the previous and next elements if the current element should be replaced:
public static int notAlone(int nums, int val) {
for(int k = 1 ; k<nums.length - 1; k++)
{
if(nums[k]==val && nums[k-1] != nums[k] && nums[k] != nums[k+1])
nums[k] = Math.max (nums[k-1], nums[k+1]);
}
return nums;
}
BTW, in your solution you ignore the given value (val
):
Return a version of the given array where every instance of the given value which is alone is replaced...
That's not the reason why your code failed in the given case, though. You simply didn't find the correct maximum:
When k==3
:
int max = nums[k]; // max = 2
if(nums[k-1]>nums[k]) // 3 > 2
max = nums[k-1]; // max = 3
else if(nums[k+1] > nums[k]) // no evaluated. therefore you change num[3] to 3 instead of to 5
max = nums[k+1];
If you replaced these 5 lines with:
int max = nums[k-1];
if(nums[k+1] > max)
max = nums[k+1];
you would have gotten the correct output.
You're over complicating it. You only need to find the max
of the previous and next elements if the current element should be replaced:
public static int notAlone(int nums, int val) {
for(int k = 1 ; k<nums.length - 1; k++)
{
if(nums[k]==val && nums[k-1] != nums[k] && nums[k] != nums[k+1])
nums[k] = Math.max (nums[k-1], nums[k+1]);
}
return nums;
}
BTW, in your solution you ignore the given value (val
):
Return a version of the given array where every instance of the given value which is alone is replaced...
That's not the reason why your code failed in the given case, though. You simply didn't find the correct maximum:
When k==3
:
int max = nums[k]; // max = 2
if(nums[k-1]>nums[k]) // 3 > 2
max = nums[k-1]; // max = 3
else if(nums[k+1] > nums[k]) // no evaluated. therefore you change num[3] to 3 instead of to 5
max = nums[k+1];
If you replaced these 5 lines with:
int max = nums[k-1];
if(nums[k+1] > max)
max = nums[k+1];
you would have gotten the correct output.
edited Nov 24 '18 at 16:02
answered Nov 24 '18 at 15:51
EranEran
282k37456543
282k37456543
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53459768%2fjava-codingbat-notalone-why-doesnt-it-work-for-this-specific-example%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
nums[k+1] > nums[k]
andnums[k-1]>nums[k]
. You are assumingnums[k]
is currently the largest number. Change them tonums[k+1]>max
andnums[k-1]>max
accordingly.– Andrew Tobilko
Nov 24 '18 at 16:03
You wrote
if-else-if
assuming that ifnums[k-1]>max
istrue
, thennums[k+1]>max
can't betrue
. Removeelse
, change it to twoif
s.– Andrew Tobilko
Nov 24 '18 at 16:04