C++ Program To Find The Winner Of A Unique Building Game

There are many exciting challenges in the field of competitive programming, and one such challenge is to decide who wins a special building game. In this game, players choose which building to add to their collection as they go through a variety of buildings, each with a unique height. The problem is that a player can only choose a building that is strictly taller than every other building they have already chosen. The winner is the player with the greatest number of buildings in their collection after the game ends, and no more choices can be made.

Assume that there are n elements in the array A. In this array, A[i] was selected by the i-th participant out of n total participants. Such a player who won the bidding contest selected a unique and small number. The player who won the game must have their index found. If it is not feasible, return -1. The proposal is a C++ program that will automate this process and determine the winner of this strategic game.

Problem statement:

There are numerous approaches for handling different programming problems. Generating an algorithm is the first step in solving a problem, and it requires a thorough analysis of the given problem. Recursive approaches are sometimes used when a problem keeps coming up; iterative structures are also sometimes utilized in these circumstances. Control statements like if-else and switch cases can be utilized to regulate the program's logic flow. Effective use of variables and data structures can result in the creation of a program that is small and requires little memory. We must investigate the currently employed programming techniques, such as divide and conquer, greedy programming, and dynamic programming, to ascertain whether they can. We can use some simple logic or a brute-force method to solve this problem. Go through the following contents to gain a better understanding of the methodology. The player who selected 3 can win the game, so if the input for our problem is A = [2, 3, 2, 4, 2], the result will be 1.

Steps to be followed:

Example

size: size of the list arr.
i := 0
for i from 0 to size - 1:
element := arr[i]
Increase the count of element + 1 in countMap by 1.
Set the position of element + 1 in positionMap to i.
answer := -1
for each pair in countMap:
if the value of pair is equal to 1:
Set answer to the value associated with the key pair in positionMap.
Break out of the loop.
Return answer

Example 1:

Let us take an example to find the winner of a unique building game in C++.

Example

#include <bits/stdc++.h>
using namespace std;

// Function to find the position of the unique element
int findUniqueElementPosition(const vector<int>& arr) {
    map<int, int> countMap, positionMap;
    int size = arr.size();

    // Count occurrences and store positions of elements
    for (int i = 0; i < size; i++) {
        int element = arr[i];
        countMap[element + 1]++;
        positionMap[element + 1] = i;
    }

    // Find the position of the unique element
    int answer = -1;
    for (auto& it : countMap) {
        if (it.second == 1) {
            answer = positionMap[it.first];
            break;
        }
    }

    return answer;
}

int main() {
    // Example usage
    vector<int> arr = { 2, 3, 2, 4, 2 };
    int position = findUniqueElementPosition(arr);
    cout << "Position of the unique element: " << position << endl;
    return 0;
}

Output:

Output

Position of the unique element: 1

Example 2:

Let us take another example to find the winner of a unique building game in C++.

Example

#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;

// Function to find the position of the unique element
int findUniqueElementPosition(const vector<int>& arr) {
    unordered_map<int, int> countMap, positionMap;
    int size = arr.size();

    // Count occurrences and store positions of elements
    for (int i = 0; i < size; i++) {
        int element = arr[i];
        countMap[element]++;
        positionMap[element] = i;
    }

    // Find the position of the unique element
    int answer = -1;
    for (auto& it : countMap) {
        if (it.second == 1) {
            answer = positionMap[it.first];
            break;
        }
    }

    return answer;
}

int main() {
    // Example usage
    vector<int> arr = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5 };
    int position = findUniqueElementPosition(arr);
    if (position != -1) {
        cout << "Position of the unique element: " << position << endl;
    } else {
        cout << "No unique element found!" << endl;
    }
    return 0;
}

Output:

Output

Position of the unique element: 7

Input Required

This code uses input(). Please provide values below: