java Codingbat notAlone — why doesn't it work for this specific example












4
















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!










share|improve this question

























  • 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 ifs.

    – Andrew Tobilko
    Nov 24 '18 at 16:04
















4
















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!










share|improve this question

























  • 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 ifs.

    – Andrew Tobilko
    Nov 24 '18 at 16:04














4












4








4









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!










share|improve this question

















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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] 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 ifs.

    – 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













  • 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 ifs.

    – 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 ifs.

– 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 ifs.

– Andrew Tobilko
Nov 24 '18 at 16:04












1 Answer
1






active

oldest

votes


















3














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.






share|improve this answer

























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    3














    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.






    share|improve this answer






























      3














      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.






      share|improve this answer




























        3












        3








        3







        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.






        share|improve this answer















        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 24 '18 at 16:02

























        answered Nov 24 '18 at 15:51









        EranEran

        282k37456543




        282k37456543






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks