does long long support negative values

problem : http://www.codechef.com/problems/COINS
CODE 1 : http://www.codechef.com/viewsolution/2694658
It gives correct answer
CODE 2 : http://www.codechef.com/viewsolution/2694667
It gives wrong answer

Only difference being
long long r[10000001]={-1};

long long func(int num)
{
if(num<12) return num;
if(num<=10000000&&r[num]!=-1) return r[num];

this is the snippet from second one(wrong one)
I just change (-1) to 0 and it gives correct answer why ? (only change)
Does long long not support negative values ?

1 Like

Yes it does support negative values as long as it is not appended after unsigned.
The reason for WA lies in the code here:
http://ideone.com/1jHaN0

The corrected version of the program with -1 is http://www.codechef.com/viewsolution/2694712

1 Like

@bugkiller is it that C and C++ can only initialize arrays as 0 properly and not other values or is this a special case?

@kcahdog >> This is a very common misconception. arr[siz] = {0} sets all its elements to zero, but arr[siz] = {some_val} sets only the first element to the value and rest might contain anything (garbage, zero). That’s why it is recommended to use memset() © / std :: fill() (C++)

Moreover there was a concept of designated initializer for C99, but it was removed from C++, I don’t know why. http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/designators.htm

@bugkiller! You have got it wrong in the comment. Let me use more general terms to correct it:

arr[siz] = {a1, a2, a3, ..., an} sets the first n elements of arr as the values specified, and all the remaining siz - n elements as zero (not garbage).

Here, siz must be at least as large as n. Otherwise compiler will throw an error. So,

arr[5] = {25, 37} gives {25, 37, 0, 0, 0}

arr[5] = {25} gives {25, 0, 0, 0, 0}

arr[5] = {0} gives {0, 0, 0, 0, 0}

But, arr[5] gives a list of garbage values.

Yes @tijoforyou, so I edited that comment immediately when I checked the same. Is that part still visible?

@bugkiller It still says “… but arr[siz] = {some_val} sets only the first element to the value and rest might contain anything (garbage, zero).”