bcrawford + 3 comments
Forgive me if I am mistaken, but the discussion board should be for discussion of the problems not for users to randomly post their solutions...
mjk + 3 comments
You'd be surprised to learn that there can be different solutions to the same problem.
For example, in 'Lonely Integers', I had a solution that worked. But then I saw there was a much more simpler answer that would run faster and I learned something new. :)
AllisonP + 1 comment
Totally agree, and I'd like to add that links to full submissions will not display for users who have not yet submitted a solution to the problem.
wittrup + 1 comment
How about a page that automatically picks a selection of the successful submissions? For each language, with one criteria as the shortest amount of code, another criteria being the fastest code, another one-liners etc. So that when you have submitted solutions to the problem, and are really stuck and just want to look at answers you can buy you into a page showing all that.
AnonymousNovice + 0 comments
sometimes other users provide good and much smarter solution then editorial.
JKDos + 0 comments
Sharing source code is great. Not only will you learn new ways, or even teach new ways. You also will get the lightbulb turned on.
Sometimes, the chanllenges are worded in a way that you can't understand what's being ask, or maybe you just have no clue. Coming to to discussion board, if you happen to find a solution, you can read it through and understand what's being done. AND BOOM the lightbulb comes on. Then rewrite it to your own language.
Doing so, you've just learned something new. Either way.
AnonymousNovice + 0 comments
Solution is posted for review. Somebody can suggest some improvement also.
sussmansa + 2 comments
Ohh wow, just figured it out. The first number is the number of inputs. So the example is 3 values, value1, value2, value3. The description is just bad.
anaspk + 6 comments
Exactly!! After wasting some 15-20 minutes in Java, I switched to C++ and it was done in 1 minute :(
ak278anand + 8 comments
Simple solution in java
private static long complement(long num){ return maxInt - num; }
Where long maxInt=(long)Math.pow(2, 32)-1;
csmattjohnston + 1 comment
Why is it Math.pow(2, 32)-1 instead of Math.pow(2, 31)-1?
ishtiaque_h + 1 comment
Happned to me, too. Later had to buy input-output to realize that the problem statement says input could be 2^32. That you cannot fit into an integer. Try reading the input as long. That worked for me. Thanks.
bnegrao + 1 comment
Why does the integers must be declared as "long"? why can't them just be 'int'? isn't int supposed to represent 32 bits number? ...
Leonina + 4 comments
Or if you want to do it with bitwise operators in java:
long num = in.nextLong();
long maxValue = Math.pow(2,32)-1;
System.out.println(num ^ maxValue);
kevin_lin790 + 0 comments
So clever. Would +20 it if I could.
(I made a solution in Python using bit operations, come to find out I never needed it.)
sushmabiswas7 + 0 comments
Thanks very much for this!
My initial implementation was timing out - too crude :/
This is MUCH better:
def flippingBits(n): flipped_n=(pow(2,32)-1)-n return flipped_n
emaskovsky + 5 comments
Yes indeed, the solutions in other languages seem pretty crazy.
In C++, I just do
unsigned x; cin >> x; cout << ~x << endl;
Done :D
MistaTwist + 1 comment
I tried this initially in JavaScript, and instantly failed. Good place for me to start, though, I guess!
sahildhawan22 + 2 comments
That's what I thought but I used int x, instead of unsigned. Can you explain why it didn't worked?
emaskovsky + 1 comment
Depends how you print it. The bits are likely correct, buut the interpretation of int in cout is indeed different than interpretation of unsigned.
I.e. with int, you'd need to cast it to unsigned just for the printing, for example:
int x; ... cout << static_cast<unsigned>(~x) << endl;
(and indeed if there is some number in the input which is greater than the max signed int, i.e. 2147483647, it will not work as well because of that - and such numbers are a valid input according to the description)
sahildhawan22 + 0 comments
But doing other bit manipulation questions, I have seen it correctly resembles the decimal value. Here for 5 (101), it should print 2 (010), but output I get is -1.
osoriomar + 2 comments
That looks pretty straightforward!
What is the name of the "~" expression? Reverse? Inverse?
harshitagarwal37 + 1 comment
does
~also changes the sign of the number ??
mgperson + 1 comment
It does if you're using a normal (signed) int. One bit is reserved for the sign and since you're flipping each bit you by definition are flipping the sign bit.
harshitagarwal37 + 1 comment
which bit stands for sign change ?
mgperson + 0 comments
The sign bit is always the most significant bit in the bit representation of the number. Generally the convention is to write the MSB as the leftmost, but this isn't always the case: For example, the 8 bit number 10001001 = -9. The leftmost 1 indicates a negative number.
AllisonP + 1 comment
I completed it in Java by using longs and complement addition.
~n + 0x0000000100000000L
xiaohanyu + 0 comments
Java 8 has a new
Integer/parseUnsignedIntfunction, check-
Soumyadeep_B + 3 comments
1 XOR anything gives its inverse. Hence, for 32 bit unsigned numbers , (2^32 -1) XOR 'the given number' flips each bit. I guess its possibly the fastest way to get this done.
scarpenter + 0 comments
Faster then simply using the ~ (one's complement) operator? I guess I thought maybe the compiler optimized a action so it wouldn't matter.
charvakcpatel007 + 1 comment
return ~x;
shrikanthmethre1 + 1 comment
kindly give the description properly
s1w2 + 1 comment
first you can take value from user and there is 3 point which you have to take care.... 1-input should convert into binary 2-binary value have to flipp means where 1 put 0 and where 0 put 1 3-You got flipped number now again you have to convrt it into decimal ,the ligic are... for (int i = arr.Length - 1; i >= 0; i--) {
uint x = arr[i]; uint r = x; if (x == 1) { value = Convert.ToUInt32(Math.Pow(2, i)); val += value; } else { if (r == 0) { } else { value = Convert.ToUInt32(Math.Pow(0, i)); val += value; } } }
this is in c# program ....you can do it in c and c++ Thank You Swatantra Mishra
ektajaiswal3mar + 1 comment
Can be done using NOT gate
int T; cin >> T; unsigned int *n = new unsigned int[T]; for(int i = 0; i<T; i++) { cin >> n[i]; } for(int i = 0; i<T; i++) { cout << ~n[i] << "\n"; }
keyurhariya + 0 comments
This solutions works fine but using for loops adds a lot of processor cycles. Also, no need to store all the numbers from input, you can save memory by processing one test case at a time. Alternative to same solution but speed and memory efficient:
int T; unsigned int n; cin >> T; while(T--){ cin >> n; cout << ~n << endl; }
arun_morampudi + 0 comments
just noticed.,its a single line code as the output we seek is the difference b/w (2^32)-input_n. This aint worth 40 points :P
abhishek106 + 4 comments
n = int(input()) for _ in range(n): val = int(input()) print(val^4294967295)
jairamg + 2 comments
Hi all, I am getting a 'runtime error' after code submission. I am pretty sure it is not a timing issue. Any idea why this is the case?
rkbansal83 + 0 comments
I am also getting runtime error after code submission. It works fine on my local. Submitted using java
Sort 326 Discussions, By:
Please Login in order to post a comment