We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
C# - the only problem, calculate min Multiple for 2 numbers
private static int GCD(int a, int b)
{
return a == 0 ? b : GCD(b % a, a);
}
private static List<int> CalcNext(ref int prev, List<int> lst, int cur)
{
int gcd = GCD(prev,cur);
lst.Add((prev/gcd)*(cur/gcd)*gcd);
prev = cur;
return lst;
}
public static List<int> solve(List<int> a)
{
int prev = a.ElementAt(0), prevB = prev;
List<int> ret = a.Skip(1).Aggregate(new List<int>{prev}, (lst,cur)=>CalcNext(ref prev, lst, cur)).ToList();
ret.Add(a.Last());
return ret;
}
...not the shortest code though
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
John and GCD list
You are viewing a single comment's thread. Return to all comments →
C# - the only problem, calculate min Multiple for 2 numbers
...not the shortest code though