You are not logged in. Please login at 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:



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

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.


answered 04 Nov '16, 00:55

ceilks's gravatar image

accept rate: 36%

toggle preview

Follow this question

By Email:

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



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "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:


question asked: 03 Nov '16, 23:43

question was seen: 400 times

last updated: 04 Nov '16, 00:06