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 

Example

Let us see the following implementation to get better understanding −

#include 
using namespace std;
void solve(int n){
   for (int i = (2 * n); i 

Input

4

Output

8, 10, 12, 14,
Updated on: 2022-03-15T05:50:15+05:30

376 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements