PassingCars problem in codility using Java












0















I'm doing codility tasks. I am currently on the Passing Car quest - https://app.codility.com/programmers/lessons/5-prefix_sums/passing_cars/ ;



One of the performance test, I got "WRONG ANSWER, got -1794967296 expected -1"



(The performance test name is "large_big_answer
0..01..1, length = ~100,000")



Other tests were well done



I wonder how to correct this error



Here is my code



class Solution {
public int solution(int A) {
int mul = 0;
int cnt = 0;
for(int i = 0 ; i<A.length ; i++){
if(A[i] == 0) mul++;
else cnt = cnt+mul;
}
if(cnt>1000000000) return -1;
return cnt;
}
}









share|improve this question

























  • Use BigInteger. An int can't hold such a large value.

    – Nicholas K
    Nov 13 '18 at 10:12













  • @NicholasK, this problem does not require BigInteger. The problem space is such that one bails at < the size that can be handled in an integer.

    – KevinO
    Nov 13 '18 at 10:14













  • oh I see, thank you:)

    – Analog
    Nov 14 '18 at 0:08
















0















I'm doing codility tasks. I am currently on the Passing Car quest - https://app.codility.com/programmers/lessons/5-prefix_sums/passing_cars/ ;



One of the performance test, I got "WRONG ANSWER, got -1794967296 expected -1"



(The performance test name is "large_big_answer
0..01..1, length = ~100,000")



Other tests were well done



I wonder how to correct this error



Here is my code



class Solution {
public int solution(int A) {
int mul = 0;
int cnt = 0;
for(int i = 0 ; i<A.length ; i++){
if(A[i] == 0) mul++;
else cnt = cnt+mul;
}
if(cnt>1000000000) return -1;
return cnt;
}
}









share|improve this question

























  • Use BigInteger. An int can't hold such a large value.

    – Nicholas K
    Nov 13 '18 at 10:12













  • @NicholasK, this problem does not require BigInteger. The problem space is such that one bails at < the size that can be handled in an integer.

    – KevinO
    Nov 13 '18 at 10:14













  • oh I see, thank you:)

    – Analog
    Nov 14 '18 at 0:08














0












0








0








I'm doing codility tasks. I am currently on the Passing Car quest - https://app.codility.com/programmers/lessons/5-prefix_sums/passing_cars/ ;



One of the performance test, I got "WRONG ANSWER, got -1794967296 expected -1"



(The performance test name is "large_big_answer
0..01..1, length = ~100,000")



Other tests were well done



I wonder how to correct this error



Here is my code



class Solution {
public int solution(int A) {
int mul = 0;
int cnt = 0;
for(int i = 0 ; i<A.length ; i++){
if(A[i] == 0) mul++;
else cnt = cnt+mul;
}
if(cnt>1000000000) return -1;
return cnt;
}
}









share|improve this question
















I'm doing codility tasks. I am currently on the Passing Car quest - https://app.codility.com/programmers/lessons/5-prefix_sums/passing_cars/ ;



One of the performance test, I got "WRONG ANSWER, got -1794967296 expected -1"



(The performance test name is "large_big_answer
0..01..1, length = ~100,000")



Other tests were well done



I wonder how to correct this error



Here is my code



class Solution {
public int solution(int A) {
int mul = 0;
int cnt = 0;
for(int i = 0 ; i<A.length ; i++){
if(A[i] == 0) mul++;
else cnt = cnt+mul;
}
if(cnt>1000000000) return -1;
return cnt;
}
}






java performance






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 10:12









Bas de Groot

540115




540115










asked Nov 13 '18 at 10:05









AnalogAnalog

81




81













  • Use BigInteger. An int can't hold such a large value.

    – Nicholas K
    Nov 13 '18 at 10:12













  • @NicholasK, this problem does not require BigInteger. The problem space is such that one bails at < the size that can be handled in an integer.

    – KevinO
    Nov 13 '18 at 10:14













  • oh I see, thank you:)

    – Analog
    Nov 14 '18 at 0:08



















  • Use BigInteger. An int can't hold such a large value.

    – Nicholas K
    Nov 13 '18 at 10:12













  • @NicholasK, this problem does not require BigInteger. The problem space is such that one bails at < the size that can be handled in an integer.

    – KevinO
    Nov 13 '18 at 10:14













  • oh I see, thank you:)

    – Analog
    Nov 14 '18 at 0:08

















Use BigInteger. An int can't hold such a large value.

– Nicholas K
Nov 13 '18 at 10:12







Use BigInteger. An int can't hold such a large value.

– Nicholas K
Nov 13 '18 at 10:12















@NicholasK, this problem does not require BigInteger. The problem space is such that one bails at < the size that can be handled in an integer.

– KevinO
Nov 13 '18 at 10:14







@NicholasK, this problem does not require BigInteger. The problem space is such that one bails at < the size that can be handled in an integer.

– KevinO
Nov 13 '18 at 10:14















oh I see, thank you:)

– Analog
Nov 14 '18 at 0:08





oh I see, thank you:)

– Analog
Nov 14 '18 at 0:08












1 Answer
1






active

oldest

votes


















2














The issue, as @Nicholas K noted, does have to do with an overflow.



Move the check for the if (cnt > 1_000_000_000) into the for loop. The requirements are:




The function should return −1 if the number of pairs of passing
cars exceeds 1,000,000,000.




As such, as soon as the number of pairs exceeds the count, then stop.



So,



public int solution(int A) {
int mul = 0;
int cnt = 0;
for(int i = 0 ; i<A.length ; i++){
if(A[i] == 0) mul++;
else cnt = cnt+mul;

if(cnt>1000000000) return -1;
}

return cnt;
}


Here is a test case that shows the failure:



@Test
public void testHalfEach() {
final int inp = new int[100_000];
final int exp = -1;
Arrays.fill(inp, 0, 50_000, 0);
Arrays.fill(inp, 50_000, 100_000, 1);
validate(inp, exp);
}

private void validate(int inp, int exp)
{
PassingCars prog = new PassingCars();
int ans = prog.solution(inp);
assertEquals(exp, ans);
}


Changing the location of the check will allow this test to pass.






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%2f53278472%2fpassingcars-problem-in-codility-using-java%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









    2














    The issue, as @Nicholas K noted, does have to do with an overflow.



    Move the check for the if (cnt > 1_000_000_000) into the for loop. The requirements are:




    The function should return −1 if the number of pairs of passing
    cars exceeds 1,000,000,000.




    As such, as soon as the number of pairs exceeds the count, then stop.



    So,



    public int solution(int A) {
    int mul = 0;
    int cnt = 0;
    for(int i = 0 ; i<A.length ; i++){
    if(A[i] == 0) mul++;
    else cnt = cnt+mul;

    if(cnt>1000000000) return -1;
    }

    return cnt;
    }


    Here is a test case that shows the failure:



    @Test
    public void testHalfEach() {
    final int inp = new int[100_000];
    final int exp = -1;
    Arrays.fill(inp, 0, 50_000, 0);
    Arrays.fill(inp, 50_000, 100_000, 1);
    validate(inp, exp);
    }

    private void validate(int inp, int exp)
    {
    PassingCars prog = new PassingCars();
    int ans = prog.solution(inp);
    assertEquals(exp, ans);
    }


    Changing the location of the check will allow this test to pass.






    share|improve this answer




























      2














      The issue, as @Nicholas K noted, does have to do with an overflow.



      Move the check for the if (cnt > 1_000_000_000) into the for loop. The requirements are:




      The function should return −1 if the number of pairs of passing
      cars exceeds 1,000,000,000.




      As such, as soon as the number of pairs exceeds the count, then stop.



      So,



      public int solution(int A) {
      int mul = 0;
      int cnt = 0;
      for(int i = 0 ; i<A.length ; i++){
      if(A[i] == 0) mul++;
      else cnt = cnt+mul;

      if(cnt>1000000000) return -1;
      }

      return cnt;
      }


      Here is a test case that shows the failure:



      @Test
      public void testHalfEach() {
      final int inp = new int[100_000];
      final int exp = -1;
      Arrays.fill(inp, 0, 50_000, 0);
      Arrays.fill(inp, 50_000, 100_000, 1);
      validate(inp, exp);
      }

      private void validate(int inp, int exp)
      {
      PassingCars prog = new PassingCars();
      int ans = prog.solution(inp);
      assertEquals(exp, ans);
      }


      Changing the location of the check will allow this test to pass.






      share|improve this answer


























        2












        2








        2







        The issue, as @Nicholas K noted, does have to do with an overflow.



        Move the check for the if (cnt > 1_000_000_000) into the for loop. The requirements are:




        The function should return −1 if the number of pairs of passing
        cars exceeds 1,000,000,000.




        As such, as soon as the number of pairs exceeds the count, then stop.



        So,



        public int solution(int A) {
        int mul = 0;
        int cnt = 0;
        for(int i = 0 ; i<A.length ; i++){
        if(A[i] == 0) mul++;
        else cnt = cnt+mul;

        if(cnt>1000000000) return -1;
        }

        return cnt;
        }


        Here is a test case that shows the failure:



        @Test
        public void testHalfEach() {
        final int inp = new int[100_000];
        final int exp = -1;
        Arrays.fill(inp, 0, 50_000, 0);
        Arrays.fill(inp, 50_000, 100_000, 1);
        validate(inp, exp);
        }

        private void validate(int inp, int exp)
        {
        PassingCars prog = new PassingCars();
        int ans = prog.solution(inp);
        assertEquals(exp, ans);
        }


        Changing the location of the check will allow this test to pass.






        share|improve this answer













        The issue, as @Nicholas K noted, does have to do with an overflow.



        Move the check for the if (cnt > 1_000_000_000) into the for loop. The requirements are:




        The function should return −1 if the number of pairs of passing
        cars exceeds 1,000,000,000.




        As such, as soon as the number of pairs exceeds the count, then stop.



        So,



        public int solution(int A) {
        int mul = 0;
        int cnt = 0;
        for(int i = 0 ; i<A.length ; i++){
        if(A[i] == 0) mul++;
        else cnt = cnt+mul;

        if(cnt>1000000000) return -1;
        }

        return cnt;
        }


        Here is a test case that shows the failure:



        @Test
        public void testHalfEach() {
        final int inp = new int[100_000];
        final int exp = -1;
        Arrays.fill(inp, 0, 50_000, 0);
        Arrays.fill(inp, 50_000, 100_000, 1);
        validate(inp, exp);
        }

        private void validate(int inp, int exp)
        {
        PassingCars prog = new PassingCars();
        int ans = prog.solution(inp);
        assertEquals(exp, ans);
        }


        Changing the location of the check will allow this test to pass.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 10:20









        KevinOKevinO

        3,09131628




        3,09131628






























            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%2f53278472%2fpassingcars-problem-in-codility-using-java%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

            The Sandy Post

            Danny Elfman

            Pages that link to "Head v. Amoskeag Manufacturing Co."