
- 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 sequence of indices of the team members
Suppose we have an array A with n elements and a number k. There are n students in a class. The rating of ith student is A[i]. We have to form a team with k students such that rating of all team members are distinct. If not possible return "Impossible", otherwise return the sequence of indices.
So, if the input is like A = [15, 13, 15, 15, 12]; k = 3, then the output will be [1, 2, 5].
Steps
To solve this, we will follow these steps −
Define two large arrays app and ans and fill them with cnt := 0 n := size of A for initialize i := 1, when i <= n, update (increase i by 1), do: a := A[i - 1] if app[a] is zero, then: (increase app[a] by 1) increase cnt by 1; ans[cnt] := i if cnt >= k, then: for initialize i := 1, when i <= k, update (increase i by 1), do: print ans[i] Otherwise return "Impossible"
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; void solve(vector<int> A, int k) { int app[101] = { 0 }, ans[101] = { 0 }, cnt = 0; int n = A.size(); for (int i = 1; i <= n; i++) { int a = A[i - 1]; if (!app[a]) { app[a]++; ans[++cnt] = i; } } if (cnt >= k) { for (int i = 1; i <= k; i++) cout << ans[i] << ", "; } else cout << "Impossible"; } int main() { vector<int> A = { 15, 13, 15, 15, 12 }; int k = 3; solve(A, k); }
Input
{ 15, 13, 15, 15, 12 }, 3
Output
1, 2, 5,
- Related Articles
- C# Program to find the sum of a sequence
- Program to find sum of given sequence in C++
- C# Program to find the average of a sequence of numeric values
- Structure and Members of the C# Program
- C++ program to find indices of soldiers who can form reconnaissance unit
- How to Make Project Team Members Perform Well?
- C++ Program to Find the Longest Prefix Matching of a Given Sequence
- C++ Program to Find the Longest Increasing Subsequence of a Given Sequence
- C++ Program to Access private members of a class
- Techniques for Developing and Mentoring Team Members
- Important Skills for Dealing with Individual Team Members
- Program to find minimum possible difference of indices of adjacent elements in Python
- Write a C program to display the size and offset of structure members
- Structure and Members of the Java Program
- C# Program to invert the order of elements in a sequence

Advertisements