Hi, I was solving this problem a few minutes ago, and the following problem is a subproblem of that one.
Given integer $$$n$$$, $$$ n \ge 3$$$, and array $$$a$$$ consisting of $$$n - 1$$$ integers, all of them are equal to some value $$$b$$$. Later, some integer $$$c$$$, such that $$$c \ne b$$$ is selected and inserted into the array $$$a$$$ at random position. By given $$$n$$$ and $$$a$$$ find $$$c$$$.
This problem itself isn't so difficult, but with a certain restriction I couldn't solve it. The restriction is folowing: you can't use $$$if$$$ and ternary operators in any way.
I came up with the solution, that calls $$$if$$$ only one time.
Can you solve this problem with the given restriction?
UPD: In the comments there is one of the solutions, and I've also came up with one.
BledDest's solution is shorter, but mine is easier to understand, at least for me, this is just
.
Auto comment: topic has been updated by tredsused70 (previous revision, new revision, compare).
Sort any three elements, then let $$$x$$$ second element. $$$c=x\oplus\sum_aa\oplus x$$$
I think this will find $$$b$$$, not $$$c$$$
Thanks for the answer, but I can't really accept it. First, sorting has $$$if$$$ in itself, and second, this approach finds b, and not c. After edit it finds c, but still sorting requires $$$if$$$.
Might be overkill, but zero if/else statements.
I believe $$$sort$$$ has $$$if$$$ in it, as well as $$$map$$$.
I bet using
while
when it is supposed to beif
isn't gonna cut it?Yeah
Is
!!
also banned?Thank you for your solution, this is what I've searched for.
I'm pretty late but yeah.
Thank you, pretty tricky solution.
Ooops looks like my solution is wrong when $$$n$$$ is even and $$$a_1=c$$$, for example:
4
1 2 1 1
I guess it doesn't matter anymore, but here is corrected version:
Another solution:
Very cool. I like this solution a lot. It is very simple to understand, even for me, and very slick as well.
The problem is you can't really say if something is an
if
.(flag ? x : y)
is equivalent tox * flag + y * !flag
, for example.