I found this problem somewhere in a contest and haven't been able to come up with a solution yet.
I have the positive integers. I have to find longest subset that among each two neighbour elements one divides another.
What I'm doing is: I'm creating the graph.Then I'm connecting nodes in which numbers divides each others. After that I'm using DFS
(one node can be connected with two nodes).
But not all test cases are true in system. Do I have to sort the array before using DFS
? Maybe there is special (Dynamic) algorithm?
Failing test cases:
N = 5
1 1 3 7 13
My code gives the output 4
. But if I arrange
this array like this:
3 1 7 1 13
The output is 5 and it's the true answer.
I also used recursive method. But I need something faster.
But not all test cases are true
Please specify failing test case.1, 2, 3
I canarrange
it and have better solution.