-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayReduction.cs
More file actions
41 lines (38 loc) · 1.24 KB
/
ArrayReduction.cs
File metadata and controls
41 lines (38 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
namespace DA.Algorithms.Problems
{
public static class ArrayReduction
{
/// <summary>
/// Given an array of positive elements.
/// You need to perform reduction operation.In each reduction operation
/// smallest positive element value is picked and all the elements are
/// subtracted by that value. You need to print the number
/// of elements left after each reduction process.
/// </summary>
/// Example:
/// Input:
/// [5, 1, 1, 1, 2, 3, 5]
/// Output:
/// 4 corresponds to [4, 1, 2, 4]
/// 3 corresponds to [3, 1, 3]
/// 2 corresponds to [2, 2]
/// 0 corresponds to [0]
public static void Reduction (int[] source)
{
Array.Sort (source);
int count = 1;
int reduction = source[0];
for (int i = 0; i < source.Length; i++)
{
if (source[i] - reduction > 0)
{
Console.WriteLine (source.Length - i);
reduction = source[i];
++count;
}
}
Console.WriteLine ("Total number of reductions: {0}", count);
}
}
}