The smallest subsequence
Practice
3 (8 votes)
Graphs
Algorithms
Breadth First search
C++
Problem
64% Success 1123 Attempts 50 Points 1.5s Time Limit 256MB Memory 1024 KB Max Code
You are given an array \(A\) that contains \(N\) integer elements. The value \(A[i]\) of every element is such that the number of divisors of \(A[i]\) is at most 7.
Find the length of the smallest non-empty subsequence of this array such that the product of elements in the subsequence is a perfect square. If no such subsequence exists, then print -1.
A sequence \(A\) is a subsequence of an array \(B\) if \(A\) can be obtained from \(B\) by deleting several (possibly, zero or all) elements.
Input format
- The first line contains an integer \(N\) denoting the number of elements in the array.
- The second line contains \(N\) space-separated integers denoting array \(A\).
Output format
Print an integer, denoting the length of the smallest non-empty subsequence of the array that satisfies the mentioned conditions.
Constraints
\(1 \le N \le 10^5 \\ 1 \le A[i] \le 10^6\)
Submissions
Please login to view your submissions
Similar Problems
Points:50
35 votes
Tags:
MediumShortest Path Algorithm
Points:50
33 votes
Tags:
Breadth First SearchGraphsMedium
Points:50
11 votes
Tags:
GraphsAlgorithmsBreadth-first searchTree
Editorial