Coding Problem #2

Menu

PROBLEM #2

Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i.

For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be [2, 3, 6].

Follow-up: what if you can’t use division?

APPROACH:

As a Mental Exercise… here’s what I’d to do approach this:

I feel like I’m gonna need a Outer Loop that iterates through the array and just keeps the position.  Then an inner loop will go through all the numbers except for the position held in the outer loop.  The answer will be stored in a output array and returned when all the loops are done.

Looking forward to it’s implementation!

SOLUTION:

So the solution was pretty much what I thought it was, I needed an outer loop to pass through the array and hold the index positon, while the inner loop multiplies the remaining elements together. In my initial solution, I printed the output to the console, but I did NOT create a new array as the problem required. Therefore I have added my corrected code below and 2nd video.

using System;
namespace DailyCodePractice002
{
    class Program
    {
        static void Main(string[] args)
        {
            // [1,2,3,4,5]  output [120,60,40,30,24]
            int[] array = new int[] {3,2,1 };
            int[] newarray = new int[array.Length];
            for (int i = 0; i < array.Length; i++)
            {
                //Console.WriteLine(array[i]);
                int accumulator = 0;
                for (int j = 0; j < array.Length; j++)
                {
                    if (i == j)
                    {
                    }
                    else
                    {
                        if (accumulator==0)
                        {
                            accumulator = array[j];
                        }
                        else
                        {
                            accumulator *= array[j];
                        }
                    }
                }
                Console.Write(accumulator+”, “);
            }
        }
    }
}
using System;
namespace DailyCodePractice002
{
    class Program
    {
        static void Main(string[] args)
        {
            // [1,2,3,4,5]  output [120,60,40,30,24]
            int[] array = new int[] { 1, 2, 3, 4, 5 };
            int[] newarray = new int[array.Length];
            for (int i = 0; i < array.Length; i++)
            {
                //Console.WriteLine(array[i]);
                int accumulator = 0;
                for (int j = 0; j < array.Length; j++)
                {
                    if (i == j)
                    {
                    }
                    else
                    {
                        if (accumulator == 0)
                        {
                            accumulator = array[j];
                        }
                        else
                        {
                            accumulator *= array[j];
                        }
                    }
                }
                newarray[i] = accumulator;
                //Console.Write(accumulator+”, “);
            }
            Console.WriteLine(“[{0}]”, string.Join(“, “, newarray));
        }
    }
}


Leave a Reply