C++ Program to find winner of unique bidding game

C++Server Side ProgrammingProgramming

<p>Suppose we have an array A with n elements. There are n participants, the i-th participant chose the number A[i]. The winner of the bidding game is such a participant that the number he chose is unique and is minimal. We have to find the index of the participant who won the game. If not possible, return -1.</p><h2>Problem Category</h2><p>Various problems in programming can be solved through different techniques. To solve a problem, we have to devise an algorithm first and to do that we have to study the particular problem in detail. A recursive approach can be used if there is a recurring appearance of the same problem over and over again; alternatively, we can use iterative structures also. Control statements such as if-else and switch cases can be used to control the flow of logic in the program. Efficient usage of variables and data structures provides an easier solution and a lightweight, low-memory-requiring program. We have to look at the existing programming techniques, such as Divide-and-conquer, Greedy Programming, Dynamic Programming, and find out if they can. This problem we can solve by some basic logics or brute-force approach. Follow the following contents to understand the approach better.</p><p>So, if the input of our problem is like A = [2, 3, 2, 4, 2], then the output will be 1, because the participant who has chosen 3 can win the game.</p><h2>Steps</h2><p>To solve this, we will follow these steps &minus;</p><pre class="just-code notranslate language-cpp" data-lang="cpp">Define one map cnt, and another map pos, both for integer type key and integer type value n := size of A for initialize i := 0, when i &lt; n, update (increase i by 1), do: &nbsp; &nbsp;x := A[i] &nbsp; &nbsp;increase cnt[x + 1] by 1 &nbsp; &nbsp;pos[x + 1] = i ans := -1 for each key-value pair it in cnt, do &nbsp; &nbsp;if value of it is same as 1, then: &nbsp; &nbsp; &nbsp; ans := pos[key of it] &nbsp; &nbsp; &nbsp; Come out from the loop return ans</pre><h2>Example</h2><p>Let us see the following implementation to get better understanding &minus;</p><pre class="demo-code notranslate language-cpp" data-lang="cpp">#include &lt;bits/stdc++.h&gt; using namespace std; int solve(vector&lt;int&gt; A){ &nbsp; &nbsp;map&lt;int, int&gt; cnt, pos; &nbsp; &nbsp;int n = A.size(); &nbsp; &nbsp;for (int i = 0; i &lt; n; i++){ &nbsp; &nbsp; &nbsp; int x = A[i]; &nbsp; &nbsp; &nbsp; cnt[x + 1]++; &nbsp; &nbsp; &nbsp; pos[x + 1] = i; &nbsp; &nbsp;} &nbsp; &nbsp;int ans = -1; &nbsp; &nbsp;for (auto it : cnt){ &nbsp; &nbsp; &nbsp; if (it.second == 1){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ans = pos[it.first]; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;} &nbsp; &nbsp;return ans; } int main(){ &nbsp; &nbsp;vector&lt;int&gt; A = { 2, 3, 2, 4, 2 }; &nbsp; &nbsp;cout &lt;&lt; solve(A) &lt;&lt; endl; }</pre><h2>Input</h2><pre class="result notranslate">{ 2, 3, 2, 4, 2 }</pre><h2>Output</h2><pre class="result notranslate">1</pre>
Updated on 08-Apr-2022 08:09:40