PassingCars problem in codility using Java
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
add a comment |
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
UseBigInteger. Anintcan't hold such a large value.
– Nicholas K
Nov 13 '18 at 10:12
@NicholasK, this problem does not requireBigInteger. 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
add a comment |
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
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
java performance
edited Nov 13 '18 at 10:12
Bas de Groot
540115
540115
asked Nov 13 '18 at 10:05
AnalogAnalog
81
81
UseBigInteger. Anintcan't hold such a large value.
– Nicholas K
Nov 13 '18 at 10:12
@NicholasK, this problem does not requireBigInteger. 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
add a comment |
UseBigInteger. Anintcan't hold such a large value.
– Nicholas K
Nov 13 '18 at 10:12
@NicholasK, this problem does not requireBigInteger. 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
add a comment |
1 Answer
1
active
oldest
votes
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.
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%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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 13 '18 at 10:20
KevinOKevinO
3,09131628
3,09131628
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%2f53278472%2fpassingcars-problem-in-codility-using-java%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
Use
BigInteger. Anintcan'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