# Generate a N sized Array of unique elements with of GCD of adjacent pairs as X

Given, two integers **N** (always even) and** X**, the task is to find an array of size** N** with distinct numbers, such that the sum of **GCD** of adjacent pairs (where each element is part of only one pair) is **X**. If no such array is possible return -1.

**Note:** If there are more than 1 possible sequence return any of them

**Examples :**

Input:N = 6, X = 6Output:4 8 9 10 11 12Explanation:Starting with first two numbers, gcd of (4, 8) = 4, then gcd of (9, 10) = 1, gcd of (11, 12) = 1.

Thus, gcd sums up to 4 + 1 + 1 = 6 which is equal to X.

Input:N = 4, X = 1Output:-1

**Approach: **The idea is that

If the value of

Xis less thanN/2, it’s obvious that it’s not possible to make a minimumGCDof N/2.

So, ifX < N/2,answer = -1.Otherwise, lets denote

extra= X – N/2 + 1, which signifies how much is the difference betweenXandsum(sum = the sum of GCD when all the adjacent pairs have GCD = 1 except for the first one) i.e. this must be GCD of the first pair

Thus, based upon the above fact,extra+ sum of GCD of other N/2-1 pairs would be equal toX.

The formula for extra can be derived as shown below:

For N numbers there are a total of N/2 pairs.

If the first pair is left and all the other pairs are made to have a GCD of 1.

So the total sum of GCDs of these pairs are (N/2 -1)*1 = (N/2 -1)So

extra= X – (N/2 – 1) = X – N/2 + 1

Follow the steps mentioned below to solve the problem:

- Make the first pair as
**extra**and**(extra*2)**. - Make the other pairs made up of consecutive integers such that they have GCD = 1.

Below is the implementation of the above approach:

## C++

`// C++ Program of the above approach.` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to get the` `// Resultant array` `void` `solve(` `int` `n, ` `int` `x)` `{` ` ` `// Formula generated to calculate` ` ` `// Extra gcd required` ` ` `int` `extra = x - n / 2 + 1;` ` ` `// Print extra required in the` ` ` `// Starting of the array sequence` ` ` `cout << extra << ` `" "` `;` ` ` `// Multiplied starting element by 2` ` ` `// Such that after taking pair of both` ` ` `// Starting and next - starting` ` ` `// Element it will be equal to extra` ` ` `extra = extra * 2;` ` ` `// Printing the leftover pairs` ` ` `// Or elements with gcd = 1` ` ` `for` `(` `int` `i = 1; i < n; i++) {` ` ` `cout << (extra++) << ` `" "` `;` ` ` `}` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `N = 4, X = 3;` ` ` `solve(N, X);` ` ` `return` `0;` `}` |

## Java

`// Java Program of the above approach.` `import` `java.util.*;` `public` `class` `GFG {` ` ` `// Function to get the` ` ` `// Resultant array` ` ` `static` `void` `solve(` `int` `n, ` `int` `x)` ` ` `{` ` ` `// Formula generated to calculate` ` ` `// Extra gcd required` ` ` `int` `extra = x - n / ` `2` `+ ` `1` `;` ` ` `// Print extra required in the` ` ` `// Starting of the array sequence` ` ` `System.out.print(extra + ` `" "` `);` ` ` `// Multiplied starting element by 2` ` ` `// Such that after taking pair of both` ` ` `// Starting and next - starting` ` ` `// Element it will be equal to extra` ` ` `extra = extra * ` `2` `;` ` ` `// Printing the leftover pairs` ` ` `// Or elements with gcd = 1` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) {` ` ` `System.out.print((extra++) + ` `" "` `);` ` ` `}` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String args[])` ` ` `{` ` ` `int` `N = ` `4` `, X = ` `3` `;` ` ` `solve(N, X);` ` ` `}` `}` `// This code is contributed by Samim Hossain Mondal.` |

## Python3

`# Python code for the above approach` `# Function to get the` `# Resultant array` `def` `solve( n, x):` ` ` `# Formula generated to calculate` ` ` `# Extra gcd required` ` ` `extra ` `=` `x ` `-` `(` `int` `)(n ` `/` `2` `) ` `+` `1` `;` ` ` `# Print extra required in the` ` ` `# Starting of the array sequence` ` ` `print` `(extra, end` `=` `" "` `);` ` ` `# Multiplied starting element by 2` ` ` `# Such that after taking pair of both` ` ` `# Starting and next - starting` ` ` `# Element it will be equal to extra` ` ` `extra ` `=` `extra ` `*` `2` `;` ` ` `# Printing the leftover pairs` ` ` `# Or elements with gcd = 1` ` ` `for` `i ` `in` `range` `(` `1` `,n` `+` `1` `):` ` ` `print` `(extra, end ` `=` `" "` `);` ` ` `extra ` `=` `extra ` `+` `1` `;` `# Driver Code` `N ` `=` `4` `X ` `=` `3` `solve(N, X);` ` ` `# This code is contributed by Potta Lokesh` |

## C#

`// C# Program of the above approach.` `using` `System;` `class` `GFG {` ` ` `// Function to get the` ` ` `// Resultant array` ` ` `static` `void` `solve(` `int` `n, ` `int` `x)` ` ` `{` ` ` `// Formula generated to calculate` ` ` `// Extra gcd required` ` ` `int` `extra = x - n / 2 + 1;` ` ` `// Print extra required in the` ` ` `// Starting of the array sequence` ` ` `Console.Write(extra + ` `" "` `);` ` ` `// Multiplied starting element by 2` ` ` `// Such that after taking pair of both` ` ` `// Starting and next - starting` ` ` `// Element it will be equal to extra` ` ` `extra = extra * 2;` ` ` `// Printing the leftover pairs` ` ` `// Or elements with gcd = 1` ` ` `for` `(` `int` `i = 1; i <= n; i++) {` ` ` `Console.Write((extra++) + ` `" "` `);` ` ` `}` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `Main()` ` ` `{` ` ` `int` `N = 4, X = 3;` ` ` `solve(N, X);` ` ` `}` `}` `// This code is contributed by Samim Hossain Mondal.` |

## Javascript

`<script>` ` ` `// JavaScript Program of the above approach.` ` ` `// Function to get the` ` ` `// Resultant array` ` ` `const solve = (n, x) => {` ` ` ` ` `// Formula generated to calculate` ` ` `// Extra gcd required` ` ` `let extra = x - parseInt(n / 2) + 1;` ` ` `// Print extra required in the` ` ` `// Starting of the array sequence` ` ` `document.write(`${extra} `);` ` ` `// Multiplied starting element by 2` ` ` `// Such that after taking pair of both` ` ` `// Starting and next - starting` ` ` `// Element it will be equal to extra` ` ` `extra = extra * 2;` ` ` `// Printing the leftover pairs` ` ` `// Or elements with gcd = 1` ` ` `for` `(let i = 1; i < n; i++) {` ` ` `document.write(`${(extra++)} `);` ` ` `}` ` ` `}` ` ` `// Driver Code` ` ` `let N = 4, X = 3;` ` ` `solve(N, X);` `// This code is contributed by rakeshsahni` `</script>` |

**Output**

2 4 5 6 7

**Time complexity: **O(N)**Space complexity:** O(1)