int a = Integer.MIN_VALUE;
int b = -a;
System.out.println("a = "+a + " | b = "+b);
Result :
a = -2147483648 | b = -2147483648
I was expecting b to be a positive number.
One of the tricky parts of this question is that Java has multiple data types to support numbers like byte, short, char, int, long, float, and double, out of those all are signed except char, which can not represent negative numbers.
On all machines, variables of the float, double, and long double data types can store positive or negative numbers.
Method 1 (Simple : O(n2)): The idea is to use two nested loop. For each element arr[i], find negative of arr[i] from index i + 1 to n – 1 and store it in another array. For output, print negative positive value of the stored element.
A number of the "int" type in Java can range from -2,147,483,648 up to 2,147,483,647.
Changing the sign of Integer.MIN_VALUE
produces an overflow; you're seeing the result of that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With