Ezraft and Array
Description:
You are given a single integer . Construct an array of distinct positive integers such that for all (), is divisible by , or determine that no such array exists.
Input Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first and only line of each test case contains a single integer ().
Output For each test case, if there is no solution, output a single integer .
Otherwise, output integers () — an array satisfying the conditions.
If there are multiple solutions, print any of them.
Video Explanation
Approaches:
1. Constructive Pattern (Optimal)
This is a classic constructive algorithm problem. Let's analyze small values of :
- For : The array
[1]works perfectly since divides . - For : We need two distinct integers such that both divide . This means must divide and must divide , which is only mathematically possible if . Since the integers must be strictly distinct, there is no solution for .
- For : The array
[1, 2, 3]has a total sum of . Since , , and all evenly divide , this is a valid base solution!
From onwards, we can build the rest of the array inductively.
If we already have a valid array (like [1, 2, 3]), its sum is . If we append exactly to the array as the next element, the new total sum becomes .
Since every previous element successfully divided , they will definitely divide . And our newly added element obviously divides as well.
Algorithm:
- Handle base cases: if , return
[1]. If , return-1. - Start with the base array
[1, 2, 3]for . The running sum is . - For each subsequent element from to , append the current running sum to the array, and then double the running sum for the next iteration.
- Constraint Check: For , the maximum value in the array would be , which is approximately . This easily fits within the constraint limit, meaning standard 64-bit integers (
long long/BigInt) are sufficient.
Complexity
- Time Complexity: per testcase, where is the desired length of the array. We construct the array in a single linear pass.
- Space Complexity: to store the array elements before printing the result.
Solutions
- C++
- Java
- Python
- JavaScript
#include <iostream>
#include <vector>
using namespace std;
void solve() {
int n;
cin >> n;
if (n == 1) {
cout << 1 << "\n";
return;
}
if (n == 2) {
cout << -1 << "\n";
return;
}
vector<long long> a = {1, 2, 3};
long long current_sum = 6;
for (int i = 3; i < n; i++) {
a.push_back(current_sum);
current_sum *= 2;
}
for (int i = 0; i < n; i++) {
cout << a[i] << (i == n - 1 ? "" : " ");
}
cout << "\n";
}
int main() {
// Fast I/O
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
import java.util.Scanner;
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
if (scanner.hasNextInt()) {
int t = scanner.nextInt();
while (t-- > 0) {
solve(scanner);
}
}
}
}
private static void solve(Scanner scanner) {
int n = scanner.nextInt();
if (n == 1) {
System.out.println(1);
return;
}
if (n == 2) {
System.out.println(-1);
return;
}
ArrayList<Long> a = new ArrayList<>();
a.add(1L);
a.add(2L);
a.add(3L);
long currentSum = 6;
for (int i = 3; i < n; i++) {
a.add(currentSum);
currentSum *= 2;
}
for (int i = 0; i < n; i++) {
System.out.print(a.get(i) + (i == n - 1 ? "" : " "));
}
System.out.println();
}
}
import sys
def solve():
input_data = sys.stdin.read().split()
if not input_data:
return
t = int(input_data[0])
for i in range(1, t + 1):
n = int(input_data[i])
if n == 1:
print(1)
continue
if n == 2:
print(-1)
continue
a = [1, 2, 3]
current_sum = 6
for _ in range(3, n):
a.append(current_sum)
current_sum *= 2
print(*(a))
if __name__ == "__main__":
solve()
const fs = require('fs');
function main() {
const input = fs.readFileSync(0, 'utf-8').trim().split(/\s+/);
if (input.length === 0 || input[0] === '') return;
let t = parseInt(input[0], 10);
for (let i = 1; i <= t; i++) {
let n = parseInt(input[i], 10);
if (n === 1) {
console.log(1);
continue;
}
if (n === 2) {
console.log(-1);
continue;
}
// Using BigInt to safely handle 64-bit integers
let a = [1n, 2n, 3n];
let currentSum = 6n;
for (let j = 3; j < n; j++) {
a.push(currentSum);
currentSum *= 2n;
}
console.log(a.join(' '));
}
}
main();
Done with this topic? Mark it as complete to track your progress.
Related Practice Problems
Handpicked problems sharing similar algorithmic topic tags
farmpiggie and Subset Sum
Solution for Codeforces 2246A: farmpiggie and Subset Sum, utilizing a parity observation approach.
Yaroslav and Productivity
Solution for Codeforces 2244D: Yaroslav and Productivity, utilizing block independence and greedy logic.
An Alternative Way
Solution for Codeforces 2241D: An Alternative Way, utilizing a prefix sum invariant approach.
💬 Discuss this page
Have a question or spot something confusing in "Ezraft and Array"? Ask below — it's backed by GitHub Discussions, so maintainers get notified like any other GitHub activity.