Skip to content

Instantly share code, notes, and snippets.

@Ezeji
Last active May 2, 2021 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ezeji/09db38ec2a593337d8e5778373131148 to your computer and use it in GitHub Desktop.
Save Ezeji/09db38ec2a593337d8e5778373131148 to your computer and use it in GitHub Desktop.
This algorithm focuses on finding the product of array elements.
class Program
{
private static int TotalNumber { get; set; }
private static List<int> Result { get; set; } = new List<int>();
static void Main(string[] args)
{
TotalNumber = 1;
int[] nums = { 4, 5, 10, 2 };
ArrayElementsMultiplication(nums);
var result = ArrayProducts(nums);
foreach (var item in result)
{
Console.WriteLine("ArrayElement:" + " " + item.ToString());
}
}
public static void ArrayElementsMultiplication(int[] nums)
{
if(nums == null) { }
else
{
for (int i = 0; i < nums.Length; i++)
{
TotalNumber *= nums[i];
}
}
}
public static List<int> ArrayProducts(int[] nums)
{
if (nums == null)
return null;
else
{
for (int i = 0; i < nums.Length; i++)
{
Result.Add(TotalNumber / nums[i]);
}
}
return Result;
}
}
@meekg33k
Copy link

meekg33k commented May 2, 2021

Hello @Ezeji, thank you for participating in Week 4 of Algorithm Fridays.

This is a decent solution and works for most of the test cases. However, I noticed you added the if (nums == null) check on line 25 and 40 inside the loop. What this means is that every time the loop runs, you would be doing that check. I think a better way would be to do the check outside the loop so you do it just once.

What do you think?

@Ezeji
Copy link
Author

Ezeji commented May 2, 2021

Hi @meekg33k, thank you for your feedback.

I just updated my code on your observation which was an oversight.

Thanks once again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment