You are not logged in. Please login at www.codechef.com to post your questions!

×

Unusual garbage value answers in SPOJ question INVCNT solution

I was solving this question: INVCNT on SPOJ.

I simply implemented the Merge Sort variant that counts inversions as well: First solution.

This returns a correct solution for the all the test cases I could think of, but then I realized that inversions could go beyond 10^8, so I changed this implementation to use long long instead. Second solution

Now weirdly, this solution returns a different value every time I execute it. I have initialized all variables. For example, trying this test case:

1

5
5
4
3
2
1

The answer should be 10, and the int solution does get this correct, but the long long solution keeps changing its answer every time I execute it. It usually gives answers in the range 13, 14, 15

I am fairly new to C++, please help me debug this. Is my solution correct? Why is the value changing every time even though the variables seem to be initialized?

Thanks for the help! :)

asked 03 Nov '16, 23:43

svineet's gravatar image

3★svineet
362410
accept rate: 0%

I'm using GCC version 6.2.1 on linux if it matters.

(04 Nov '16, 00:06) svineet3★

Array index out of bounds. Check what is happening in the loop in MergeCount if lptr==m or rptr==n.

link

answered 04 Nov '16, 00:55

ceilks's gravatar image

7★ceilks
1.8k9
accept rate: 36%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,901
×145
×136
×47

question asked: 03 Nov '16, 23:43

question was seen: 400 times

last updated: 04 Nov '16, 00:06