Efficient way to generate combinations ordered by increasing sum of indexes - c#

For a heuristic algorithm I need to evaluate, one after the other, the combinations of a certain set until I reach a stop criterion.
Since they are a lot, at the moment I'm generating them using the following memory efficient iterator block (inspired by python's itertools.combinations):
public static IEnumerable<T[]> GetCombinations<T>(this IList<T> pool, int r)
{
int n = pool.Count;
if (r > n)
throw new ArgumentException("r cannot be greater than pool size");
int[] indices = Enumerable.Range(0, r).ToArray();
yield return indices.Select(idx => pool[idx]).ToArray();
while (true)
{
int i;
for (i = r - 1; i >= 0; i--)
if (indices[i] != i + n - r)
break;
if (i < 0)
break;
indices[i] += 1;
for (int j = i + 1; j < r; j++)
indices[j] = indices[j - 1] + 1;
yield return indices.Select(idx => pool[idx]).ToArray();
}
}
The problem is, to greatly improve the efficiency of my heuristic, I'd need to generate these combinations sorted by the sum of they indexes (in other words I need to generate first, the combinations containing the first elements of the set).
e.g.
Consider the set S = {0,1,2,3,4,5}
(I choose this set for simplicity since elements and their indexes coincide).
All possible combinations of r=4 numbers generated from the given algorithm are:
(0, 1, 2, 3) SUM: 6
(0, 1, 2, 4) SUM: 7
(0, 1, 2, 5) SUM: 8
(0, 1, 3, 4) SUM: 8
(0, 1, 3, 5) SUM: 9
(0, 1, 4, 5) SUM: 10
(0, 2, 3, 4) SUM: 9
(0, 2, 3, 5) SUM: 10
(0, 2, 4, 5) SUM: 11
(0, 3, 4, 5) SUM: 12
(1, 2, 3, 4) SUM: 10
(1, 2, 3, 5) SUM: 11
(1, 2, 4, 5) SUM: 12
(1, 3, 4, 5) SUM: 13
(2, 3, 4, 5) SUM: 14
where, as you can see, the combinations are not strictly sorted by ascending sum.
The desired outcome is instead the following :
(the order of the combinations having the same sum is not important)
(0, 1, 2, 3) SUM: 6
(0, 1, 2, 4) SUM: 7
(0, 1, 2, 5) SUM: 8
(0, 1, 3, 4) SUM: 8
(0, 1, 3, 5) SUM: 9
(0, 2, 3, 4) SUM: 9
(0, 1, 4, 5) SUM: 10
(0, 2, 3, 5) SUM: 10
(1, 2, 3, 4) SUM: 10
(0, 2, 4, 5) SUM: 11
(1, 2, 3, 5) SUM: 11
(0, 3, 4, 5) SUM: 12
(1, 2, 4, 5) SUM: 12
(1, 3, 4, 5) SUM: 13
(2, 3, 4, 5) SUM: 14
A trivial solution would be to generate all the combinations then sort them according to their sum; but this is not really efficient/feasible since the number of combinations becomes huge as n grows.
I also had a quick look to combinatorial Gray Codes but I couldn't find anyone suitable for this problem.
Do you have an idea on how to implement something like this ?
EDIT :
This problem has an alternate (unfortunately not easier) formulation.
Given a set S and a number r, all the possible sums are trivial to find, since they are simply all the numbers from the sum of the first r elements of S to the sum of the last r elements of S.
That being said, if, for each sum T we can efficiently¹ find all the combinations having sum T we solve the original problem since we simply generate them in ascending order.
¹ efficiently means that I don't want to generate all the combinations and discard the ones having a different sum.
EDIT 2:
After #EricLippert suggestion I created the following code:
public static IEnumerable<T[]>
GetCombinationsSortedByIndexSum<T>(this IList<T> pool, int r)
{
int n = pool.Count;
if (r > n)
throw new ArgumentException("r cannot be greater than pool size");
int minSum = ((r - 1) * r) / 2;
int maxSum = (n * (n + 1)) / 2 - ((n - r - 1) * (n - r)) / 2;
for (int sum = minSum; sum <= maxSum; sum++)
{
foreach (var indexes in AllMonotIncrSubseqOfLenMWhichSumToN(0, n - 1, r, sum))
yield return indexes.Select(x => pool[x]).ToArray();
}
}
static IEnumerable<IEnumerable<int>>
AllMonotIncrSubseqOfLenMWhichSumToN(int seqFirstElement, int seqLastElement, int m, int n)
{
for (int i = seqFirstElement; i <= seqLastElement - m + 1; i++)
{
if (m == 1)
{
if (i == n)
yield return new int[] { i };
}
else
{
foreach (var el in AllMonotIncrSubseqOfLenMWhichSumToN(i + 1, seqLastElement, m - 1, n - i))
yield return new int[] { i }.Concat(el);
}
}
}
This works fine (hopefully is what Eric meant :P) but I'm still concerned about the complexity of the recursive method. In fact it seems that we're regenerating all the combinations for each sum discarding the ones not summing up to the desired value.
To reduce the complexity of the inner function I found a way to limit the iterations by using effective upper and lower bounds (and now it's really hard to say what is the complexity of this).
Check my answer to see the final code.

The solution I had in mind was:
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
// Preconditions:
// * items is a sequence of non-negative monotone increasing integers
// * n is the number of items to be in the subsequence
// * sum is the desired sum of that subsequence.
// Result:
// A sequence of subsequences of the original sequence where each
// subsequence has n items and the given sum.
static IEnumerable<IEnumerable<int>> M(IEnumerable<int> items, int sum, int n)
{
// Let's start by taking some easy outs. If the sum is negative
// then there is no solution. If the number of items in the
// subsequence is negative then there is no solution.
if (sum < 0 || n < 0)
yield break;
// If the number of items in the subsequence is zero then
// the only possible solution is if the sum is zero.
if (n == 0)
{
if (sum == 0)
yield return Enumerable.Empty<int>();
yield break;
}
// If the number of items is less than the required number of
// items, there is no solution.
if (items.Count() < n)
yield break;
// We have at least n items in the sequence, and
// and n is greater than zero, so First() is valid:
int first = items.First();
// We need n items from a monotone increasing subsequence
// that have a particular sum. We might already be too
// large to meet that requirement:
if (n * first > sum)
yield break;
// There might be some solutions that involve the first element.
// Find them all.
foreach(var subsequence in M(items.Skip(1), sum - first, n - 1))
yield return new[]{first}.Concat(subsequence);
// And there might be some solutions that do not involve the first element.
// Find them all.
foreach(var subsequence in M(items.Skip(1), sum, n))
yield return subsequence;
}
static void Main()
{
int[] x = {0, 1, 2, 3, 4, 5};
for (int i = 0; i <= 15; ++i)
foreach(var seq in M(x, i, 4))
Console.WriteLine("({0}) SUM {1}", string.Join(",", seq), i);
}
}
The output is your desired output.
I've made no attempt to optimize this. It would be interesting to profile it and see where most of the time is spent.
UPDATE: Just for fun I wrote a version that uses an immutable stack instead of an arbitrary enumerable. Enjoy!
using System;
using System.Collections.Generic;
using System.Linq;
abstract class ImmutableList<T> : IEnumerable<T>
{
public static readonly ImmutableList<T> Empty = new EmptyList();
private ImmutableList() {}
public abstract bool IsEmpty { get; }
public abstract T Head { get; }
public abstract ImmutableList<T> Tail { get; }
public ImmutableList<T> Push(T newHead)
{
return new List(newHead, this);
}
private sealed class EmptyList : ImmutableList<T>
{
public override bool IsEmpty { get { return true; } }
public override T Head { get { throw new InvalidOperationException(); } }
public override ImmutableList<T> Tail { get { throw new InvalidOperationException(); } }
}
private sealed class List : ImmutableList<T>
{
private readonly T head;
private readonly ImmutableList<T> tail;
public override bool IsEmpty { get { return false; } }
public override T Head { get { return head; } }
public override ImmutableList<T> Tail { get { return tail; } }
public List(T head, ImmutableList<T> tail)
{
this.head = head;
this.tail = tail;
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public IEnumerator<T> GetEnumerator()
{
for (ImmutableList<T> current = this; !current.IsEmpty; current = current.Tail)
yield return current.Head;
}
}
class Program
{
// Preconditions:
// * items is a sequence of non-negative monotone increasing integers
// * n is the number of items to be in the subsequence
// * sum is the desired sum of that subsequence.
// Result:
// A sequence of subsequences of the original sequence where each
// subsequence has n items and the given sum.
static IEnumerable<ImmutableList<int>> M(ImmutableList<int> items, int sum, int n)
{
// Let's start by taking some easy outs. If the sum is negative
// then there is no solution. If the number of items in the
// subsequence is negative then there is no solution.
if (sum < 0 || n < 0)
yield break;
// If the number of items in the subsequence is zero then
// the only possible solution is if the sum is zero.
if (n == 0)
{
if (sum == 0)
yield return ImmutableList<int>.Empty;
yield break;
}
// If the number of items is less than the required number of
// items, there is no solution.
if (items.Count() < n)
yield break;
// We have at least n items in the sequence, and
// and n is greater than zero.
int first = items.Head;
// We need n items from a monotone increasing subsequence
// that have a particular sum. We might already be too
// large to meet that requirement:
if (n * first > sum)
yield break;
// There might be some solutions that involve the first element.
// Find them all.
foreach(var subsequence in M(items.Tail, sum - first, n - 1))
yield return subsequence.Push(first);
// And there might be some solutions that do not involve the first element.
// Find them all.
foreach(var subsequence in M(items.Tail, sum, n))
yield return subsequence;
}
static void Main()
{
ImmutableList<int> x = ImmutableList<int>.Empty.Push(5).
Push(4).Push(3).Push(2).Push(1).Push(0);
for (int i = 0; i <= 15; ++i)
foreach(var seq in M(x, i, 4))
Console.WriteLine("({0}) SUM {1}", string.Join(",", seq), i);
}
}

For the sake of completeness and clarity I'll post my final code:
// Given a pool of elements returns all the
// combinations of the groups of lenght r in pool,
// such that the combinations are ordered (ascending) by the sum of
// the indexes of the elements.
// e.g. pool = {A,B,C,D,E} r = 3
// returns
// (A, B, C) indexes: (0, 1, 2) sum: 3
// (A, B, D) indexes: (0, 1, 3) sum: 4
// (A, B, E) indexes: (0, 1, 4) sum: 5
// (A, C, D) indexes: (0, 2, 3) sum: 5
// (A, C, E) indexes: (0, 2, 4) sum: 6
// (B, C, D) indexes: (1, 2, 3) sum: 6
// (A, D, E) indexes: (0, 3, 4) sum: 7
// (B, C, E) indexes: (1, 2, 4) sum: 7
// (B, D, E) indexes: (1, 3, 4) sum: 8
// (C, D, E) indexes: (2, 3, 4) sum: 9
public static IEnumerable<T[]>
GetCombinationsSortedByIndexSum<T>(this IList<T> pool, int r)
{
int n = pool.Count;
if (r > n)
throw new ArgumentException("r cannot be greater than pool size");
int minSum = F(r - 1);
int maxSum = F(n) - F(n - r - 1);
for (int sum = minSum; sum <= maxSum; sum++)
{
foreach (var indexes in AllSubSequencesWithGivenSum(0, n - 1, r, sum))
yield return indexes.Select(x => pool[x]).ToArray();
}
}
// Given a start element and a last element of a sequence of consecutive integers
// returns all the monotonically increasing subsequences of length "m" having sum "sum"
// e.g. seqFirstElement = 1, seqLastElement = 5, m = 3, sum = 8
// returns {1,2,5} and {1,3,4}
static IEnumerable<IEnumerable<int>>
AllSubSequencesWithGivenSum(int seqFirstElement, int seqLastElement, int m, int sum)
{
int lb = sum - F(seqLastElement) + F(seqLastElement - m + 1);
int ub = sum - F(seqFirstElement + m - 1) + F(seqFirstElement);
lb = Math.Max(seqFirstElement, lb);
ub = Math.Min(seqLastElement - m + 1, ub);
for (int i = lb; i <= ub; i++)
{
if (m == 1)
{
if (i == sum) // this check shouldn't be necessary anymore since LB/UB should automatically exclude wrong solutions
yield return new int[] { i };
}
else
{
foreach (var el in AllSubSequencesWithGivenSum(i + 1, seqLastElement, m - 1, sum - i))
yield return new int[] { i }.Concat(el);
}
}
}
// Formula to compute the sum of the numbers from 0 to n
// e.g. F(4) = 0 + 1 + 2 + 3 + 4 = 10
static int F(int n)
{
return (n * (n + 1)) / 2;
}

Related

Pairs of amicable numbers

I have a task to find pairs of amicable numbers and I've already solved it. My solution is not efficient, so please help me to make my algorithm faster.
Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number. The smallest pair of amicable numbers is (220, 284). They are amicable because the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110, of which the sum is 284; and the proper divisors of 284 are 1, 2, 4, 71 and 142, of which the sum is 220.
Task: two long numbers and find the first amicable numbers between them. Let s(n) be the sum of the proper divisors of n:
For example:
s(10) = 1 + 2 + 5 = 8
s(11) = 1
s(12) = 1 + 2 + 3 + 4 + 6 = 16
If s(firstlong) == s(secondLong) they are amicable numbers
My code:
public static IEnumerable<long> Ranger(long length) {
for (long i = 1; i <= length; i++) {
yield return i;
}
}
public static IEnumerable<long> GetDivisors(long num) {
return from a in Ranger(num/2)
where num % a == 0
select a;
}
public static string FindAmicable(long start, long limit) {
long numberN = 0;
long numberM = 0;
for (long n = start; n <= limit; n++) {
long sumN = GetDivisors(n).Sum();
long m = sumN;
long sumM = GetDivisors(m).Sum();
if (n == sumM ) {
numberN = n;
numberM = m;
break;
}
}
return $"First amicable numbers: {numberN} and {numberM}";
}
I generally don't write C#, so rather than stumble through some incoherent C# spaghetti, I'll describe an improvement in C#-madeup-psuedo-code.
The problem seems to be in your GetDivisors function. This is linear O(n) time with respect to each divisor n, when it could be O(sqrt(n)). The trick is to only divide up to the square root, and infer the rest of the factors from that.
GetDivisors(num) {
// same as before, but up to sqrt(num), plus a bit for floating point error
yield return a in Ranger((long)sqrt(num + 0.5)) where num % a == 0
if ((long)sqrt(num + 0.5) ** 2 == num) { // perfect square, exists
num -= 1 // don't count it twice
}
// repeat, but return the "other half"- num / a instead of a
yield return num/a in Ranger((long)sqrt(num + 0.5)) where num % a == 0
}
This will reduce your complexity of that portion from O(n) to O(sqrt(n)), which should provide a noticeable speedup.
There is a simple formula giving the sum of divisors of a number knowing its prime decomposition:
let x = p1^a1 * ... * pn^an, where pi is a prime for all i
sum of divisors = (1+p1+...+p1^a1) * ... (1+pn+...+pn^an)
= (1-p1^(a1+1))/(1-p1) * ... ((1-pn^(an+1))/(1-pn)
In order to do a prime decomposition you must compute all prime numbers up to the square root of the maximal value in your search range. This is easily done using the sieve of Erathostenes.

If exit array, enter from the other side

How can I do this for example if I have 1, 2, 3, 4, 5, 6, 7 array and I am in 4th position (number 5) and if you have to move it to the right 4 positions you should be in position 1 (number 2). The same goes with negative numbers but you move to left. I guess there is a need of while(true) loop?
Lets assume i is the index and n is the size of the array.
For positive i the needed index = i%n
For negative i i%n returns negative residue, so the needed index is n+i%n
You can use
int index(int i, int n) {
return i%n < 0 ? n + (i%n) : i%n;
}
You can calculate your index like this:
var newIndex = (index + 4) % 7;
So the fourth position becomes (4+4) % 7 or 1.
Always clearer with named functions and followable code path instead of voodoo one liners that work
public void MyTest()
{
var testData = new[] { 1, 2, 3, 4, 5, 6, 7 };
Assert.AreEqual(2, TraverseCircularArray(testData, 5, 3));
Assert.AreEqual(6, TraverseCircularArray(testData, 2, -4));
}
private int TraverseCircularArray(int[] array, int currentIndex, int interval)
{
var i = array[currentIndex];
if (currentIndex + interval < 0)
i = array[array.Length + (interval + currentIndex)];
else if (currentIndex + interval >= array.Length)
i = array[currentIndex - interval - 1];
else
i = array[currentIndex + interval];
return i;
}

Find all the addition and subtraction combinations from an array of numbers

I need to make a function to take in an array of numbers and a target number and return how many different ways you can add or subtract those numbers to get the target number.
ie.
Values = 2, 4, 6, 8 Target = 12
2 + 4 + 6 = 12,
4 + 8 = 12,
6 + 8 - 2 = 12,
2 - 4 + 6 + 8 = 12,
Return 4
Here is what I have so far, but it only counts addition problems.
private void RecursiveSolve(int goal, int currentSum, List<int> included, List<int> notIncluded, int startIndex)
{
for (int index = startIndex; index < notIncluded.Count; index++)
{
int nextValue = notIncluded[index];
if (currentSum + nextValue == goal)
{
List<int> newResult = new List<int>(included);
newResult.Add(nextValue);
mResults.Add(newResult);
}
else if (currentSum - nextValue == goal)
{
List<int> newResult = new List<int>(included);
newResult.Add(nextValue);
mResults.Add(newResult);
}
if (currentSum - nextValue < goal && currentSum - nextValue > 0 )
{
List<int> nextIncluded = new List<int>(included);
nextIncluded.Add(nextValue);
List<int> nextNotIncluded = new List<int>(notIncluded);
nextNotIncluded.Remove(nextValue);
RecursiveSolve(goal, currentSum - nextValue, nextIncluded, nextNotIncluded, startIndex++);
}
if (currentSum + nextValue < goal)
{
List<int> nextIncluded = new List<int>(included);
nextIncluded.Add(nextValue);
List<int> nextNotIncluded = new List<int>(notIncluded);
nextNotIncluded.Remove(nextValue);
RecursiveSolve(goal, currentSum + nextValue, nextIncluded, nextNotIncluded, startIndex++);
}
}
}
Well, the simple way would be to try all of the combinations. If you have N numbers, you have 3^N combinations. The reasoning is this: You sum the numbers but put a coefficient in front of each of them. If your numbers are A1..AN, you add N coefficients (C1..CN) and sum:
Sum (Ai*Ci)
Your Cis can be 1 (meaning you add the number), -1 (meaning you subtract the number) or 0 (meaning you ignore the number).
So, go over all 3^N possible coefficient assignments, calculate the sum and compare to your target.
I am assuming all the numbers are different (as in your example). If a number can appear twice, you need to take that into account.

construct an array of integers to achieve specific sequence

construct the shortest possible sequence of integers ending with A,
using the following rules:
the first element of the sequence is 1, each of the successive
elements is the sum of any two preceding elements (adding a single
element to itself is also permissible), each element is larger than
all the preceding elements; that is, the sequence is increasing.
For example, for A = 42, a possible solutions is [1, 2, 3, 6, 12, 24,
30, 42]. Another possible solution is [1, 2, 4, 5, 8, 16, 21, 42].
I have written the following but it fails on input of 456, by returning[1,2,4,8,16,32,64,128,200,256,456] , there are no numbers in the sequence that can be added together to get 200.
how can I fix the below code? what am I doing wrong?
public static int[] hit(int n)
{
List<int> nums = new List<int>();
int x = 1;
while (x < n)
{
nums.Add(x);
x = x * 2;
if (x > n)
{
nums.Add(n - (x / 2));
nums.Add(n);
}
}
nums.Sort();
int[] arr = nums.ToArray();
return arr;
}
I know there is gonna be a mathematical proof behind this, but my guess would be along the lines of dividing the number by 2, if it divides equally, repeat the process. If the there is a remainder, it would be 1. So you would have the integer quotient and the quotient plus one. Since one is guaranteed to be in the set, the larger of the 2 numbers is already taken care of. So just repeat the process for the smaller. This problem certainly implies a recursive solution that should be relatively trivial, so I will leave that up to the poster to implement.
I think I got it:
public Set<Integer> shortList(int n){
Set<Integer> result = new HashSet<Integer>();
Stack<Integer> stack = new Stack<Integer>();
result.add(n);
int num=n, den=0;
while(num>1){
while(num > den){
num--; den++;
if(num%den==0)
stack.push(num);
}//num>den
if(!stack.isEmpty()){
num = stack.pop();
result.add(num);
stack.clear();
}else{
result.add(num);
result.add(den);
}
den=0;
}
return result;
}//
Results (unsorted)
for 42: [1, 2, 3, 21, 6, 7, 42, 14]
for 15: [1, 2, 4, 5, 10, 15]
for 310: [1, 2, 155, 4, 5, 310, 10, 124, 62, 31, 15, 30]
Here is my solution in C++ (may be trivially changed to C#):
void printSequenceTo(unsigned n)
{
if (n == 1) { printf("1"); return; }
if (n & 1) {
int factor = 3;
do {
if (n % factor == 0) {
printSequenceTo(n / factor * (factor-1));
factor = 0;
break;
}
factor += 2;
} while (factor * factor <= n);
if (factor) printSequenceTo(n-1);
}
else
printSequenceTo(n/2);
printf(",%u", n);
}
Demonstration: http://ideone.com/8lXxc
Naturally it could be sped up using a sieve for factorization.
Note, this is significant improvement over the accepted answer, but it still is not optimal.
Here is my attempt. It may be optimised, but it shows my idea:
private static IEnumerable<int> OptimalSequence(int lastElement)
{
var result = new List<int>();
int currentElement = 1;
do
{
result.Add(currentElement);
currentElement = currentElement * 2;
} while (currentElement <= lastElement);
var realLastElement = result.Last();
if (lastElement != realLastElement)
{
result.Add(lastElement);
FixCollection(result, lastElement - realLastElement);
}
return result;
}
private static void FixCollection(List<int> result, int difference)
{
for (int i = 0; i < result.Count; i++)
{
if (result[i] == difference) break;
if (result[i] > difference)
{
result.Insert(i, difference);
FixCollection(result, difference - result[i-1]);
break;
}
}
}
Edit
I can't prove it formally but my answer and Chris Gessler's answer give sequences of the same size (at least I checked for numbers between 1 and 10000) because both algorithms compensate odd numbers.
Some examples:
Number 1535
1,2,3,4,7,8,15,16,31,32,63,64,127,128,255,256,511,512,1024,1535
Number 2047
1,2,3,4,7,8,15,16,31,32,63,64,127,128,255,256,511,512,1023,1024,2047
Number 3071
1,2,3,4,7,8,15,16,31,32,63,64,127,128,255,256,511,512,1023,1024,2048,3071
Number 4095
1,2,3,4,7,8,15,16,31,32,63,64,127,128,255,256,511,512,1023,1024,2047,2048,4095
Number 6143
1,2,3,4,7,8,15,16,31,32,63,64,127,128,255,256,511,512,1023,1024,2047,2048,4096,6143
Number 8191
1,2,3,4,7,8,15,16,31,32,63,64,127,128,255,256,511,512,1023,1024,2047,2048,4095,4096,8191
==============
Number 1535
1,2,4,5,10,11,22,23,46,47,94,95,190,191,382,383,766,767,1534,1535
Number 2047
1,2,3,6,7,14,15,30,31,62,63,126,127,254,255,510,511,1022,1023,2046,2047
Number 3071
1,2,4,5,10,11,22,23,46,47,94,95,190,191,382,383,766,767,1534,1535,3070,3071
Number 4095
1,2,3,6,7,14,15,30,31,62,63,126,127,254,255,510,511,1022,1023,2046,2047,4094,4095
Number 6143
1,2,4,5,10,11,22,23,46,47,94,95,190,191,382,383,766,767,1534,1535,3070,3071,6142,6143
Number 8191
1,2,3,6,7,14,15,30,31,62,63,126,127,254,255,510,511,1022,1023,2046,2047,4094,4095,8190,8191
public static int[] hit(int n)
{
List<int> nums = new List<int>();
nums.Add(n);
int x = 0;
int Right = 0;
int Left = 0;
do
{
//even num
if (n % 2 == 0)
{
x = n / 2;
//result of division is also even 20/2 = 10
if (x % 2 == 0 || n>10 )
{
nums.Add(x);
n = x;
}
else
{
nums.Add(x + 1);
nums.Add(x - 1);
n = x - 1;
}
}
//numbers that can only be divided by 3
else if (n % 3 == 0)
{
x = n / 3;//46/3 =155
Right = x * 2;//155*2 = 310
Left = x;//155
nums.Add(Right);
nums.Add(Left);
n = x;
}
//numbers that can only be divided by 5
else
{
x = n / 2;
Right = x + 1;
Left = x;
nums.Add(Right);
nums.Add(Left);
n = Left;
}
} while (n > 2);
nums.Add(1);
nums.Reverse();
int[] arr = nums.ToArray();
return arr;
}

No of transformation steps to qualize array

Hi Can any one tell me how to solve this problem in C#.
I have an array consisting of N elements. elements in array can be positive and negative intgers.
if A=[11, 3, 7, 1]
i want to calculate minimum no of transformation steps required to make array elements equal.
each element in array can be incremented or decremented by 1.
Array A will need 5 transformation steps to get A =[6, 6, 6, 6]
In very transformation each element has to be incremented or decremented by 1.
[11, 3, 7, 1] (initial array)
[10, 4, 6, 2] (after step 1)
[9, 5, 7, 3] (after step 2)
[8, 6, 6, 4] (after step 3)
[7, 7, 5, 5] (after step 4)
[6, 6, 6, 6] (after step 5)
Some in some arrays it may not be possible.
for example it is not possible with [1,4,7] to equalise elements to one number. In Such cases it should return -1
Thanks in advance.
Well presumably you just:
Find the maximum element
Find the minimum element
The number of steps required will be half the difference between the maximum and the minimum, rounding up
You'd find the mean of the maximum and minimum element, rounding either up or down - it won't affect the number of steps - and then on each transformation step, you'd adjust each array element towards that mean.
EDIT: It's hard to see how you could get more efficient than this. In each step, the maxmimum and minimum elements can't get more than 2 closer to each other (the maximum being reduced by one, the minimum being increased by one) so the number of steps is at least half the difference, rounding up. My solution also says how you get to that state in exactly half the difference, rounding up, so it's a concrete solution, with none better.
EDIT: Here's the code to perform the transformations. Not as efficient as it can be, but it works...
using System;
using System.Linq;
class Test
{
static void Main()
{
int[] current = new[] { 1, 3, 9, 11, 5 };
// Check all odd or all even
if (current.Select(x => x % 2).Distinct().Skip(1).Any())
{
Console.WriteLine("No solution!");
return;
}
while (current != null)
{
Console.WriteLine(string.Join(" ", current));
current = Transform(current);
}
}
static int[] Transform(int[] input)
{
// We could do the "mean" calculation just once,
// but it doesn't really matter for the sake of
// demonstration
int max = input.Max();
int min = input.Min();
if (max == min)
{
// Done
return null;
}
int mean = (max + min) / 2;
return input.Select(x => x > mean ? x - 1 : x + 1)
.ToArray();
}
}
Does this work?
edit sorry, this:
public int[] Equalize(int[] arr)
{
int min = int.MaxValue;
int max = int.MinValue;
int parity = arr[0] % 2;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] % 2 != parity) return null;
if (arr[i] < min) min = arr[i];
if (arr[i] > max) max = arr[i];
}
int diff = (max - min) / 2;
int midVal = diff + min;
return arr.Select(i => midVal).ToArray();
}
Is ROUND_DOWN(SUM(each) / N) works as expected?
Java 8 version of Jon Skeet solution
public static void main(String[] args) throws FileNotFoundException {
System.out.println(getTransformationCount(new int[] {1, 3, 9, 11, 5 }));
}
public static int getTransformationCount(int[] A) {
int count = 0;
A = Transform(A);
if (IntStream.of(A).map(n -> n % 2).distinct().skip(1).count() > 0) {
return -1;
}
while (A != null) {
A = Transform(A);
count++;
}
return count;
}
public static int[] Transform(int[] input) {
int min = Arrays.stream(input).max().getAsInt();
int max = Arrays.stream(input).min().getAsInt();
if (max == min) {
return null;
}
int mean = (max + min) / 2;
return Arrays.stream(input).map((n) -> {
if (n > mean)
return n - 1;
else
return n + 1;
}).toArray();
}

Categories