12 May 2017

Make Array Consecutive 2

My solution:
int makeArrayConsecutive2(int[] statues) 
{
    return statues.Max() - statues.Min() + 1 - statues.Length;
}

Previous

Replace Middle

My solution:
int[] replaceMiddle(int[] arr) 
{
    return arr.Length % 2 == 1
                ? arr
                : arr.Select((i, i1) => i1 != arr.Length / 2 - 1 ? i : arr[arr.Length / 2 - 1] + arr[arr.Length / 2])
                    .Where((i, i1) => i1 != arr.Length / 2)
                    .ToArray();
}

Previous Next

Is Smooth?

My solution:
bool isSmooth(int[] arr) 
{
    return arr.Length % 2 == 0
                ? arr.First().Equals(arr.Last()) && arr.First().Equals(arr[arr.Length / 2 - 1] + arr[arr.Length / 2])
                : arr.First().Equals(arr.Last()) && arr.First().Equals(arr[arr.Length / 2]);
}

Previous Next

Remove Array Part

My solution:
int[] removeArrayPart(int[] inputArray, int l, int r) 
{
    return inputArray.Select(i => i).Where((i, i1) => i1 < l || i1 > r).ToArray();
}

Previous Next

Concatenate Arrays

My solution:
int[] concatenateArrays(int[] a, int[] b) 
{
    return a.Concat(b).ToArray();
}

Previous Next

First Reverse Try

My solution:
int[] firstReverseTry(int[] arr) 
{
    return arr.Select((i, i1) => i1 == 0 || i1 == arr.Length - 1 ? i1 == 0 ? arr.Last() : arr.First() : i).ToArray();
}

Previous Next

Array Replace

My solution:
int[] arrayReplace(int[] inputArray, int elemToReplace, int substitutionElem) 
{
    return inputArray.Select(i => i == elemToReplace ? substitutionElem : i).ToArray();
}

Previous Next

Create Array

My solution:
int[] createArray(int size) 
{
    return Enumerable.Repeat(1, size).ToArray();
}

Previous Next

Candles

My solution:
int candles(int candlesNumber, int makeNew) 
{
    var sum = candlesNumber;
    while (candlesNumber >= makeNew)
    {
        sum += candlesNumber / makeNew;
        candlesNumber = candlesNumber / makeNew + (candlesNumber - candlesNumber / makeNew * makeNew);
    }

    return sum;
}

Previous Next

Rounders

My solution:
int rounders(int value) 
{
    if (value < 10)
        return value;

    var ic = value.ToString().Reverse().ToList();
    var s = "0";
    for (var i = 1; i < ic.Count; i++)
    {
        if (ic[i - 1] > '4')
        {
            if (ic[i] == '9')
                ic.Add('1');

            ic[i] = ic[i] != '9' ? (char) (ic[i] + 1) : '0';
        }

        if (i != ic.Count - 1)
            s += "0";
        else if (ic[i] != '9')
            s += ic[i];
        else
            s += "01";
    }

    return int.Parse(new string(s.Reverse().ToArray()));
}

Previous Next

Increase Number Roundness

My solution:
bool increaseNumberRoundness(int n) 
{
    return n.ToString().TrimEnd('0').Contains('0');
}

Previous Next

Apple Boxes

My solution:
int appleBoxes(int k) 
{
    return Enumerable.Range(1, k).Select(i => Convert.ToInt32(Math.Pow(-1, i) * Math.Pow(i, 2))).Sum();
}

Previous Next

Addition Without Carrying

My solution:
int additionWithoutCarrying(int param1, int param2) 
{
    return (param1 / 10000 + param2 / 10000) % 10 * 10000 + (param1 / 1000 + param2 / 1000) % 10 * 1000 + (param1 / 100 + param2 / 100) % 10 * 100 + (param1 / 10 + param2 / 10) % 10 * 10 + (param1 + param2) % 10;
}

Previous Next

Lineup

My solution:
int lineUp(string commands) 
{
    var b = true;
    var count = 0;
    foreach (var c in commands)
        if ((c == 'L' || c == 'R') && b)
            b = false;
        else if ((c == 'L' || c == 'R') && !b)
        {
            b = true;
            count++;
        }
        else if (c == 'A')
            if (b)
                count++;

    return count;
}

Previous Next

Magical Well

My solution:
int magicalWell(int a, int b, int n) 
{
    var sum = 0;
    while (n != 0)
    {
        sum += a * b;
        a++;
        b++;
        n--;
    }

    return sum;
}

Previous Next

Count Sum Of Two Representations 2

My solution:
int countSumOfTwoRepresentations2(int n, int l, int r) 
{
    var count = 0;
    for (var a = l; a <= r; a++)
        if (a <= n - a && n - a >= l && n - a <= r)
            count++;

    return count;
}

Previous Next

Least Factorial

My solution:
int leastFactorial(int n) 
{
    var i = 1;
    while (Factorial(i) < n)
        i++;

    return Factorial(i);
}

int Factorial(int p0)
{
    if (p0 > 1)
        return Factorial(p0 - 1) * p0;

    return 1;
}

Previous Next