An unexpectedly difficult problem
Difference between en21 and en22, changed 0 character(s)
Hi, I was solving [this](https://codeforces.net/contest/1790/problem/C) 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.↵

<spoiler summary="my solution">~~~~~↵
#include <bits/stdc++.h>↵

using namespace std;↵

int a[100001] = {0};↵

int main()↵
{↵
    ios_base::sync_with_stdio(false);↵
    cin.tie(nullptr);↵
    int n, ans = 0;↵
    cin >> n;↵
    for(int i = 0; i < n; i++)↵
        cin >> a[i];↵
    if(a[0] == a[1])↵
        for(int i = (n & 1) ^ 1; i < n; i++)↵
            ans ^= a[i];↵
    else↵
        for(int i = 0; i < 3; i++)↵
            ans ^= a[i];↵
    cout << ans << "\n";↵
    return 0;↵
}↵
~~~~~↵
</spoiler>↵

Can you solve this problem with the given restriction?↵

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en24 English tredsused70 2023-01-28 00:38:27 5 (published)
en23 English tredsused70 2023-01-28 00:38:18 1028 (saved to drafts)
en22 English tredsused70 2023-01-27 23:56:48 0 Tiny change: 'striction?\n' -> 'striction? There is no restriction to time complexity\n'
en21 English tredsused70 2023-01-27 22:43:33 0 (published)
en20 English tredsused70 2023-01-27 22:41:31 4 Tiny change: 'blem of this one.\n\nG' -> 'blem of that one.\n\nG'
en19 English tredsused70 2023-01-27 22:39:12 46
en18 English tredsused70 2023-01-27 22:34:44 0
en17 English tredsused70 2023-01-27 22:34:44 207
en16 English tredsused70 2023-01-27 22:29:54 2
en15 English tredsused70 2023-01-27 22:29:23 122
en14 English tredsused70 2023-01-27 22:23:31 191
en13 English tredsused70 2023-01-27 22:14:54 5 Tiny change: 'h that $c != b$, and b' -> 'h that $c \ne b$, and b'
en12 English tredsused70 2023-01-27 22:11:37 28
en11 English tredsused70 2023-01-27 22:11:06 20
en10 English tredsused70 2023-01-27 22:10:01 256
en9 English tredsused70 2023-01-27 22:00:12 51
en8 English tredsused70 2023-01-27 21:58:18 13
en7 English tredsused70 2023-01-27 21:56:46 159 Tiny change: 'solution">\n~~~~~\n#in' -> 'solution">~~~~~\n#in'
en6 English tredsused70 2023-01-27 21:51:24 66
en5 English tredsused70 2023-01-27 21:50:44 49 Tiny change: '\n\n<spoil' -> '\n~~~~~\nint n = 1;\ncout << n << "\n";\n~~~~~\n\n\n<spoil'
en4 English tredsused70 2023-01-27 21:50:07 5
en3 English tredsused70 2023-01-27 21:48:58 25
en2 English tredsused70 2023-01-27 21:47:57 256
en1 English tredsused70 2023-01-27 21:46:13 122 Initial revision (saved to drafts)