Last active
May 2, 2021 16:44
-
-
Save Ezeji/09db38ec2a593337d8e5778373131148 to your computer and use it in GitHub Desktop.
This algorithm focuses on finding the product of array elements.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
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
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?