How do i solve it.

Revision en1, by attiw, 2017-11-11 12:10:47

I was solving a problem and reduced it to the following.

for each i find smallest j such that j > i and gcd(arr[i], arr[j]) == 1. 

for arr = [2, 6, 3, 2, 5]

for i = 0, j = 2
for i = 1, j = 4
for i = 2, j = 3
for i = 3, j = 4
for i = 4, j = No Found

i taught of stacks.. But don't know to to solve it.

Any solution of O(n) or O(nlogn) will be appreciated. Thank you. 
Tags gcd

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English attiw 2017-11-11 12:10:47 466 Initial revision (published)