Medium csharp

Product of array except self #20

Problem

For [1,2,3,4] style array length 6, print products without division.

Hints
  • Two passes: left products, then right

Your solution

TestStatusDetails
Ready — edit the code above and click Run or Submit.

Solution

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        int[] a = { 1, 2, 3, 4, 5, 6 };
        int len = a.Length;
        int[] outArr = new int[len];
        outArr[0] = 1;
        for (int j = 1; j < len; j++) outArr[j] = outArr[j - 1] * a[j - 1];
        int suffix = 1;
        for (int j = len - 1; j >= 0; j--) { outArr[j] *= suffix; suffix *= a[j]; }
        Console.WriteLine(string.Join(" ", outArr));
    }
}

Try solving on your own first, then reveal the official answer.

Explanation

Prefix + suffix products.

Discussion

0

Sign in to join the discussion.

No discussions yet — ask the first question!

Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details