• + 1 comment

    Find the most difficult element. Remove it and calculate gcd of the rest of elemnts.

    let X be a set of numbers. let Y be a superset of X (i.e X + {one or more elemnts} ) GCD of Y is less than or equal to GCD of X (more members less common factors, lower gcd)

    In the problem,

    Iterate and calculate gcd from left to right. Whenever a decline in gcd is observed save that as 'difficult element', continue this (update the 'difficult element' when another decline in gcd is observed - this is a more difficult element than the previous one) towards the end of elements.

    So finally we get the most difficult element. If there was no decline in gcd then that means the first element was the most difficult one. Remove that most difficult element and find gcd of the rest of the elemnts in the set. This GCD will not be a divisor of the difficult element. (That is the requirement in the question, find a number which divides all elements except one)

    Let the numbers be {a0, a1, a2, ... an} calculate gcd(a0, a1), then gcd(a0, a1, a2), then gcd(a0, a1, a2, a3) ... gcd(a0, a1,...an) whenver new gcd is less than previous gcd, save the elemnt which caused the decline.

    Make use of the associative property of GCD i.e

    gcd(a0, a1, a2) = gcd(gcd(a0, a1), a2)

    gcd(a0, a1, a2, a3) = gcd(gcd(a0, a1, a2), a3)