קוד: וועל אויס אלע
public static int solution(int A, int B, int K)
{
if (K > B && A > 0)
return 0;
else
if (K > B)//return 1 for 0 as explained below
return 1;
long count = 0;//we need to handle when B is 2 billion
for (int i = B; i >= A; i--)//start of from the highest number
{
if (i % K == 0)//else it maybe a prime
{
count = (((long)(i - A)) + K) / K; //if A is 0, add one multiple of K for 0 is divisible by all numbers. if not, still add one multiple of K for the number you’re subtracting (A).
break;
}
}
return (int)count;
}
Solution #8
קוד: וועל אויס אלע
public static int solution(int[] A)
{
HashSet<int> hs = new HashSet<int>();
var B = A.OrderBy(i => i).ToArray();
var startInd = Array.IndexOf(B, B.Where(i => i > 0).Count() > 0 ? B.First(i => i > 0) : 1);//if the array contains positive values, get the index of the first and lowest positive item
if (startInd < 0)//if there are no positive numbers, 1 will also not be found and will set startInd to -1.
return 1; ;
for (int j = startInd; j < A.Length; j++)
hs.Add(B[j]);//deals with dups
int e = 1;
while (hs.Contains(e))
{
e++;
}
return e;
}