
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to find winner of unique bidding game
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.
Problem Category
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.
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.
Steps
To solve this, we will follow these steps −
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 < n, update (increase i by 1), do: x := A[i] increase cnt[x + 1] by 1 pos[x + 1] = i ans := -1 for each key-value pair it in cnt, do if value of it is same as 1, then: ans := pos[key of it] Come out from the loop return ans
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(vector<int> A){ map<int, int> cnt, pos; int n = A.size(); for (int i = 0; i < n; i++){ int x = A[i]; cnt[x + 1]++; pos[x + 1] = i; } int ans = -1; for (auto it : cnt){ if (it.second == 1){ ans = pos[it.first]; break; } } return ans; } int main(){ vector<int> A = { 2, 3, 2, 4, 2 }; cout << solve(A) << endl; }
Input
{ 2, 3, 2, 4, 2 }
Output
1
- Related Articles
- C++ program to find winner of card game
- Program to find winner of stone game in Python
- C++ program to find winner of cell coloring game
- C++ program to find winner of ball removal game
- Program to find winner of array removal game in Python
- Program to find winner of number reducing game in Python
- C++ Program to find winner name of stick crossing game
- Program to find the winner of an array game using Python
- Program to find winner of a rower reducing game in Python
- Program to find winner of a rower breaking game in Python
- C++ program to find winner of typing game after delay timing
- Python program to find score and name of winner of minion game
- Program to find winner of a set element removal game in Python
- Predict the winner in Coin Game in C++
- Find the winner of a game where scores are given as a binary string in Python
