Showing posts with label Edge of the Ocean. Show all posts
Showing posts with label Edge of the Ocean. Show all posts

01 April 2017

matrixElementsSum

My solution:
int matrixElementsSum(int[][] matrix)
{
    int sum = 0;
    int rows = matrix.GetLength(0);
    int columns = matrix[0].Length;
    for (int x = 0; x < columns; x++)
        for (int y = 0; y < rows; y++)
        {
            if (matrix[y][x] == 0)
                break;

            sum += matrix[y][x];
        }

    return sum;
}

Previous Next

almostIncreasingSequence

My solution:
bool almostIncreasingSequence(int[] sequence)
{
    bool erased = false;
    for (int i = sequence.Length - 1; i > 0; i--)
        if (sequence[i] <= sequence[i - 1])
            if (erased != true)
                erased = true;
            else
                return false;

    return true;
}

Previous Next

Make Array Consecutive 2

My solution:
int makeArrayConsecutive2(int[] statues)
{
    int min = statues.Min();
    int max = statues.Max();
    int st = 0;
    for (int i = min; i <= max; i++)
        if (!statues.Contains(i))
            st++;

    return st;
}

Previous Next

shapeArea

My solution:
int shapeArea(int n) 
{
    return 2 * n * (n - 1) + 1;
}

Previous Next

15 February 2017

adjacentElementsProduct

My solution:
int adjacentElementsProduct(int[] inputArray) 
{
    int largest_product = int.MinValue;
    for (int i = 0; i < inputArray.Length - 1; i++)
        if (inputArray[i] * inputArray[i + 1] > largest_product)
            largest_product = inputArray[i] * inputArray[i + 1];

    return largest_product;
}

Previous Next