- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ code to kid chair association
Suppose we have a number n. We have to find an array A of size n. There are n tables and each table has 4 chairs. Chairs are numbered from 1 to 4n. It is known that two kids who sit on chairs with numbers a and b (a != b) will indulge if −
gcd(a,b) = 1 or,
a divides b or b divides a.
We want to seat the kids so there are no 2 of the kid that can indulge. More formally. We have to find the chair association.
So, if the input is like n = 4, then the output will be [14, 10, 12, 8], (other answers are also possible).
Steps
To solve this, we will follow these steps −
for initialize i := (2 * n), when i < 4 * n, update i = i + 2, do: print i
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; void solve(int n){ for (int i = (2 * n); i < 4 * n; i = i + 2){ cout << i << ", "; } } int main(){ int n = 4; solve(n); }
Input
4
Output
8, 10, 12, 14,
Advertisements