I am trying to implement a problem in Hackerrank Cut the sticks. Problem can be found here
my code is this
static int[] cutTheSticks(int[] arr)
{
int n = arr.Length, k = 0;
int[] result = new int[n];
Array.Sort(arr);
Array.Reverse(arr);
while(arr.Length != 0)
{
result[k] = arr.Length;
k++;
for(int i = 0; i < n; ++i)
{
arr[i] -= arr[arr.Length - 1];
}
}
return result;
}
it shows error as-
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at Solution.cutTheSticks (System.Int32[] arr) [0x00020] in solution.cs:24
line 24 is:
result[k] = arr.Length;
How to remove this?
There are several problems with your code. To name a few:
You are giving the result array a fixed size (int[] result=new int[n];), but it's size depends entirely on how many duplicate values are contained in the list.
You are supposed to remove from the array the smallest value(s) in each iteration. However you are just modifying the values (arr[i] -= arr[arr.Length - 1];), not removing them, so the array length will remain the same, and thus while (arr.Length != 0) will always be true, creating an endless loop. This causes k++ to keep incrementing until it reaches a value greater than the array length, which then results in the exception you are getting.
Since you are supposed to change the size of the input array, I suggest using List<int> instead, here's an example:
List<int> output = new List<int>();
List<int> input = new List<int>(arr);
while (input.Count > 0)
{
output.Add(input.Count);
int min = input.Min();
input.RemoveAll(x => x == min);
input.ForEach(x => x -= min);
}
return output.ToArray();
It is necessary to add condition k < n before assigning value to avoid IndexOutOfRangeException exception. In addition, there is a strong need a condition to avoid infinite while loop :
static int[] cutTheSticks(int[] arr) {
int n = arr.Length,
k = 0;
int[] result = new int[n];
Array.Sort(arr);
Array.Reverse(arr);
while (arr.Length != 0)
{
if (k < n)
result[k] = arr.Length;
else
break;
k++;
for (int i = 0; i < n; ++i)
{
arr[i] -= arr[arr.Length - 1];
}
}
return result;
}
UPDATE:
It is possible to pop out one element after every iteration like this:
static int[] cutTheSticks(int[] arr)
{
int n = arr.Length,
k = 0;
int[] result = new int[n];
var arrToBeRemoved = arr.ToList();
Array.Sort(arr);
Array.Reverse(arr);
while (arr.Length != 0)
{
if (k < n)
result[k] = arr.Length;
else
break;
if (k < arrToBeRemoved.Count)
arrToBeRemoved.RemoveAt(k);
arr = arrToBeRemoved.ToArray();
k++;
for (int i = 0; i < arr.Length; ++i)
{
arr[i] -= arr[arr.Length - 1];
}
}
return result;
}
I would do it that way:
static int[] cutTheSticks(int[] arr)
{
List<int> results = new List<int>();
int cutted = 0;
while (cutted != 1)
{
cutted = 0;
int min = GetMin(arr);
if (min == 0)
{
break;
}
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] >= min)
{
arr[i] -= min;
cutted++;
}
}
results.Add(cutted);
}
return results.ToArray();
}
static int GetMin(int[] arr)
{
int min = int.MaxValue;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] != 0 && arr[i] < min)
{
min = arr[i];
}
}
return min;
}
Related
I have an array of n integers and I need to divide any of it's elements by 2 (return the ceiling of the result) for k times such that the sum is minimum. The value of k can be very large as compared to n.
I am using this code:
private static int GetMaxSum(int[] array, int k)
{
int n = array.Length;
for (int i = 0; i < k; i++)
{
var indexAtMax = GetMaxIndex(array);
if (array[indexAtMax] == 1) break;
array[indexAtMax] = array[indexAtMax] / 2 + array[indexAtMax] % 2;
}
return array.Sum();
}
private static int GetMaxIndex(int[] array)
{
int maxIndex = 0;
int max = array[0];
for (int i=1; i<array.Length;i++)
{
if (array[i] > max)
{
max = array[i];
maxIndex = i;
}
}
return maxIndex;
}
How can we improve the performance further probably by using max heap or some other data structure?
Unless I'm misunderstanding your requirements, your solution seens way too complicated (and apparently wrong according to comments).
I can't really think this through right now, but wouldn't it be the case that the global solution is made up of optimal intermediate steps? The order in which you divide is irrelevant and the problem is linear.
If that is the case, you simply have to evaluate the optimal division in each step and that is not very hard to do:
static void Minimize(int[] arr, int k)
{
for (var j = 0; j < k; j++)
{
var maxGainIndex = -1;
var maxGain = int.MinValue;
for (var i = 0; i < arr.Length; i++)
{
var gain = arr[i] - (arr[i]/2 + arr[i] % 2);
if (gain > maxGain)
{
maxGain = gain;
maxGainIndex = i;
}
}
arr[maxGainIndex] -= maxGain;
}
}
If I'm not wrong, the asymptotic behavior of this algorithm is O(k·n).
UPDATE:
Based on claims of posted code being far less optimal, I've taken the liberty of benchmarking both algorithms with these results on my machine:
Input array: 100;120;80;55;75;115;125;150;90;35;65;77;89;10;11;113;200;300
Number of divisions: 20
Running benchmarks in Release mode without debugger attached.
1000000 of GetMimimum finished in 584 ms with result 704.
1000000 of GetMimimum2 finished in 8846 ms with result 704.
Benchmarking code can be found here: https://dotnetfiddle.net/ITx53q
The performance gain of my proposed algorithm is rather staggering (x15), which was expected because your solution is, as evaluated initally, overcomplicated at best for such a simple problem.
As the assumption was that k>>n, the simpler algorithms are of the order O(kn) which can be too much of iterations.
I have written this code thinking of the problem and how can I limit sorting or calculating min/max. I have divided the array into subarrays so that the operations can be performed on subarrays without thinking of the order of operations.
private static int GetMinSum(int[] array, int k)
{
int n = array.Length;
var sum = 0;
k = GetOptimizedListAndK(array, n, k, out var lists);
//If more sublists are needed
if (k > 0)
{
var count = lists.CountSum;
var key = lists.Key;
if (key > 0)
{
var poweroftwo = 1 << key;
sum += count * poweroftwo - k * poweroftwo / 2;
var dictionary2 = GetDictionary(array, lists, poweroftwo);
key = dictionary2.Keys.Last();
while (k > 0 && key > 0)
{
var list2 = dictionary2[key];
count = list2.Count;
if (k >= count)
{
list2.ForEach(
index => array[index] = array[index] / 2 + array[index] % 2);
dictionary2.Remove(key);
key = dictionary2.Keys.LastOrDefault();
k -= count;
}
else
{
if (k <= Log2(count))
{
for (int i = 0; i < k; i++)
{
var indexAtMax = GetMaxIndex(list2, array);
array[indexAtMax] = array[indexAtMax] / 2 + array[indexAtMax] % 2;
}
k = 0;
}
if (count - k <= Log2(count))
{
var minIndexes = GetMinIndexes(list2, array, count - k);
foreach (var i in list2)
{
if (!minIndexes.Contains(i))
{
array[i] = array[i] / 2 + array[i] % 2;
}
}
k = 0;
}
if (k > 0)
{
poweroftwo = 1 << key;
sum += list2.Count * poweroftwo - k * poweroftwo / 2;
dictionary2 = GetDictionary(array, list2, poweroftwo);
key = dictionary2.Keys.Last();
}
}
}
}
}
return array.Sum() + sum;
}
private static int GetOptimizedListAndK(int[] array, int n, int k, out Lists lists)
{
lists = null;
Dictionary<int, Lists> dictionary = new Dictionary<int, Lists>();
PopulatePowerBasedDictionary(array, n, dictionary);
var key = dictionary.Keys.Max();
while (key > 0 && k > 0)
{
lists = dictionary[key];
var count = lists.CountSum;
if (k >= count)
{
lists.ForEach(list => list.ForEach(index => array[index] = array[index] / 2 + array[index] % 2));
if (key > 1)
{
if (dictionary.TryGetValue(key - 1, out var lowerlists))
{
lowerlists.AddRange(lists);
lowerlists.CountSum += count;
}
else dictionary.Add((key - 1), lists);
}
dictionary.Remove(key);
key--;
k -= count;
}
else
{
if (k < Log2(count))
{
for (int i = 0; i < k; i++)
{
var indexAtMax = GetMaxIndex(lists, array);
array[indexAtMax] = array[indexAtMax] / 2 + array[indexAtMax] % 2;
}
k = 0;
}
if (count - k < Log2(count))
{
var minIndexes = GetMinIndexes(lists, array, count - k);
foreach (var list in lists)
{
foreach (var i in list)
{
if (!minIndexes.Contains(i))
{
array[i] = array[i] / 2 + array[i] % 2;
}
}
}
k = 0;
}
break;
}
}
return k;
}
private static void PopulatePowerBasedDictionary(int[] array, int n, Dictionary<int, Lists> dictionary)
{
for (int i = 0; i < n; i++)
{
if (array[i] < 2) continue;
var log2 = Log2(array[i]);
if (dictionary.TryGetValue(log2, out var lists))
{
lists[0].Add(i);
lists.CountSum++;
}
else
{
lists = new Lists(1,log2) { new List<int> { i } };
dictionary.Add(log2, lists);
}
}
}
private static int GetMaxIndex(List<int> list, int[] array)
{
var maxIndex = 0;
var max = 0;
foreach (var i in list)
{
if (array[i] > max)
{
maxIndex = i;
max = array[i];
}
}
return maxIndex;
}
private static SortedDictionary<int, List<int>> GetDictionary(int[] array, Lists lists, int poweroftwo)
{
SortedDictionary<int, List<int>> dictionary = new SortedDictionary<int, List<int>>();
foreach (var list in lists)
{
foreach (var i in list)
{
array[i] = array[i] - poweroftwo;
if (array[i] < 2)
{
continue;
}
var log2 = Log2(array[i]);
if (dictionary.TryGetValue(log2, out var list2))
{
list2.Add(i);
}
else
{
list2 = new List<int> { i };
dictionary.Add(log2, list2);
}
}
}
return dictionary;
}
private static SortedDictionary<int, List<int>> GetDictionary(int[] array, List<int> list, int poweroftwo)
{
SortedDictionary<int, List<int>> dictionary = new SortedDictionary<int, List<int>>();
foreach (var i in list)
{
array[i] = array[i] - poweroftwo;
if (array[i] < 2)
{
continue;
}
var log2 = Log2(array[i]);
if (dictionary.TryGetValue(log2, out var list2))
{
list2.Add(i);
}
else
{
list2 = new List<int> { i };
dictionary.Add(log2, list2);
}
}
return dictionary;
}
private static int GetMaxIndex(Lists lists, int[] array)
{
var maxIndex = 0;
var max = 0;
foreach (var list in lists)
{
foreach (var i in list)
{
if (array[i]>max)
{
maxIndex = i;
max = array[i];
}
}
}
return maxIndex;
}
private static HashSet<int> GetMinIndexes(Lists lists, int[] array, int k)
{
var mins = new HashSet<int>();
var minIndex = 0;
var min = int.MaxValue;
for (int j = 0; j < k; j++)
{
foreach (var list in lists)
{
foreach (var i in list)
{
if (array[i] < min && !mins.Contains(i))
{
minIndex = i;
min = array[i];
}
}
}
mins.Add(minIndex);
min = int.MaxValue;
}
return mins;
}
private static HashSet<int> GetMinIndexes(List<int> list, int[] array, int k)
{
var mins = new HashSet<int>();
var minIndex = 0;
var min = int.MaxValue;
for (int j = 0; j < k; j++)
{
foreach (var i in list)
{
if (array[i] < min && !mins.Contains(i))
{
minIndex = i;
min = array[i];
}
}
mins.Add(minIndex);
min = int.MaxValue;
}
return mins;
}
private static int Log2(int n)
{
return BitOperations.Log2((uint)n);
}
Lists Class:
public class Lists:List<List<int>>
{
public int Key { get; set; }
public int CountSum { get; set; }
public Lists(int countSum, int key):base()
{
CountSum = countSum;
Key = key;
}
}
I have a program that compares two integer value's length with this extension method
public static int NumDigits(this int n)
{
if (n < 0)
{
n = (n == int.MinValue) ? int.MaxValue : -n;
}
if (n < 10) return 1;
if (n < 100) return 2;
if (n < 1000) return 3;
if (n < 10000) return 4;
if (n < 100000) return 5;
if (n < 1000000) return 6;
if (n < 10000000) return 7;
if (n < 100000000) return 8;
return n < 1000000000 ? 9 : 10;
}
And it works perfectly. When I print the value of num1.numDigits(), the value returns 4 (it is worth '1111'. And my other integer: num2.numDigits() returns 2 (it is 11). This is great but when I actually compare them:
int[] rawNum2 = Arrays.DigitArr(num2);
if (num1.NumDigits() > num2.NumDigits())
{
int diff = num1.NumDigits() - num2.NumDigits();
for (int i = 1; i < diff; i++)
{
rawNum2.Append(0);
}
reversedNum2 = rawNum2.Reverse();
}
reversedNum2 is still '11' when it should be '0011'.
This is the class I compiled and used.
public static int[] Append(this int[] source, int value)
{
int[] newValue = source;
newValue = newValue.Concat(new[] { value }).ToArray();
return newValue;
}
public static int[] Reverse(this int[] array)
{
int[] arr = array;
for (int i = 0; i < arr.Length / 2; i++)
{
int tmp = arr[i];
arr[i] = arr[arr.Length - i - 1];
arr[arr.Length - i - 1] = tmp;
}
return arr;
}
public static int[] DigitArr(int n)
{
if (n == 0) return new int[1] { 0 };
var digits = new List<int>();
for (; n != 0; n /= 10)
digits.Add(n % 10);
var arr = digits.ToArray();
Array.Reverse(arr);
return arr;
}
Why is this happening?
You are discarding the return value of the Append method.
Change
rawNum2.Append(0);
to
rawNum2 = rawNum2.Append(0);
inside the for loop.
Your loop could be and should be simplified to:
rawNum2 = rawNum2.PadRight(num1.NumDigits(), '0')
To get the reversedNum2 as 0011 change your loop as below.
for (int i = 1; i <= diff; i++)
{
rawNum2=rawNum2.Append(0);
}
I made two changes changed the for loop to use i<=diff instead of i < diff
and assigning the return value from Append() method into the rawNum2.
I want to shift elements in one array to right by only changing the index. Also I don't want to use built-in functions
For example, if we have
8,6,5,3,9
Then We will have
9,8,6,5,3
If the array doesn't have enough length. the rest of the elements shift will start from the first index of array.
int index = 0, temp = 0;
int[] myarray = new int[int.Parse(Console.ReadLine())];
for (int i = 0; i < myarray.Length; i++)
{
myarray[i] = int.Parse(Console.ReadLine());
}
for (int i = 0; i < myarray.Length; i++)
{
if (myarray.Length <= i + 5)
{
index = ((i + 5) % 5);
temp = myarray[index];
myarray[index] = myarray[i];
myarray[i] = temp;
}
else
{
temp = myarray[i + 5];
myarray[i + 5] = myarray[i];
myarray[i] = temp;
}
}
this is what i have tried but its not working
If shift count is less than or equal array size you can use this code (I edited answer by this link):
using System;
int M = 5;//shift count
int size; //size of array
int[] myarray = new int[int.Parse(Console.ReadLine())];
for (int i = 0; i < myarray.Length; i++)
{
myarray[i] = int.Parse(Console.ReadLine());
}
size = myarray.Length;
if (size >= M)
{
Array.Reverse(myarray, 0, size);
Array.Reverse(myarray, 0, M);
Array.Reverse(myarray, M, size - M);
for (int i = 0; i < myarray.Length; i++)
{
Console.Write(myarray[i]+"\t");
}
Console.Read();
}
Basically, I'd like a set of sets that contains from (0..9), then (0, 1..9), (1, 2..9)..(8,9), and so on and so forth up until (0,1,2,3,4,5,6,7,8,9). I know this can be accomplished by nesting for loops in the manner below, but I'm wondering if there's a neater way of doing it?
Preferably something that could be accomplished within C#, but I'm interested in any algorithm.
for (int i = 0; i < max; i++) {
yield {i};
for (int j = i + 1; j < max; j++) {
yield {i, j};
for (int k = j + 1; k < max; k++) {
yield {i, j, k};
for (int l = k + 1; l < max; l++) {
yield {i, j, k, l};
for (int m = l + 1; m < max; m++) {
yield {i, j, k, l, m};
// And so on and so forth
}
}
}
}
}
I wrote this a while ago. It uses a Stack. It's generic, so it can be used for other sequences as well.
static IEnumerable<T[]> CombinationsAnyLength<T>(params T[] values)
{
Stack<int> stack = new Stack<int>(values.Length);
int i = 0;
while (stack.Count > 0 || i < values.Length) {
if (i < values.Length) {
stack.Push(i++);
int c = stack.Count;
T[] result = new T[c];
foreach (var index in stack) result[--c] = values[index];
yield return result;
} else {
i = stack.Pop() + 1;
if (stack.Count > 0) i = stack.Pop() + 1;
}
}
}
CombinationsAnyLength(1, 2, 3, 4) outputs:
1
12
123
1234
124
13
134
14
2
23
234
24
3
34
4
Why not treat this as bits and generate the set from the bits?
IEnumberable<List<int>> MakeSets()
{
// count from 1 to 2^10 - 1 (if you want the empty set, start at 0
for (uint i=1; i < (1 << 10); i++) {
// enumerate the bits as elements in a set
List<int> set = BitsIn(i);
yield return set;
}
}
List<int> MakeSet(uint i)
{
List<int> set = new List<int>();
// this will give you values from 0..max
// if you want 1, start with 1
// if you want non-integers, pass in an array of values and index into that
int val = 0;
// for every bit in i
while (i != 0)
{
// add the val if the corresponding bit is set
if ((i & 1) != 0) set.Add(val);
i = i >> 1;
val++;
}
return set;
}
and since I like the generic version above, let's make this generic too:
IEnumerable<List<T>> MakeSets(params T[] values)
{
if (values.Length > 63) throw new IllegalArgumentException("63 is the limit");
for (ulong i = i; i < (1 << (values.Length + 1); i++) {
List<T> set = new List<T>();
int val = 0;
ulong j = i;
while (j != 0) {
if ((j & 1) != 0) set.Add(values[val]);
j = j >> 1;
val++;
}
yield return set;
}
}
here is a algorithm for generating sub-sets.
let you have a set S = [a,b,c,d,e,f].
and you want to generate all the subsets then length of the array containing all the sub-sets will be
2^n where n is number of elements in S.
int A = [] // array containing all sub-sets
for i = 0 --- 2^n
x = binary(i) // like for i = 5 -> x = '000101' where x is a string of n digits.
ns = [] // new array
for j = 0 --- n
if x[j] == '1'
push S[j] into ns array
push ns into A
return A
A will have every set you wanted or you can modify it to get new result.
Using Dennis signature:
public static IEnumerable<T[]> CombinationsAnyLength<T>(params T[] values)
{
for(var i = 0; i < (1 << values.Length); i++)
{
var result = new List<T>();
for(var j = 0; j < values.Length; j++)
{
if(((1 << j) & i) != 0)
{
result.Add(values[j]);
}
}
yield return result.ToArray();
}
}
I can't use a built-in function for this, I must use my own logic.
I've done element shifting to the left side, but the right side doesn't work for me. Not sure why.
My method for left:
public int[] shiftLeft(int[] arr) {
int[] demo = new int[arr.length];
int index = 0;
for (int i = 0; i < arr.length - 1; i++) {
demo[index] = arr[i + 1];
index++;
}
return demo;
}
and my attempt for the right shifting:
public int[] shiftRight(int[] arr) {
int[] demo = new int[arr.length];
int index = 0;
for (int i = arr.length - 1; i >= 0; i--) {
demo[index] = arr[(i - 1 > 0) ? i - 1 : 0];
index++;
}
return demo;
}
What am I doing wrong?
By shifting I mean:
you have an array, 1 2 3 4 5 6
Shifting it to left by one: 2 3 4 5 6 1
Shifting it to right by one: 6 1 2 3 4 5
//right shift with modulus
for (int i = 0; i < arr.length; i++) {
demo[(i+1) % demo.length] = arr[i];
}
The easiest way to go:
public int[] shiftLeft(int[] arr)
{
int[] demo = new int[arr.Length];
for (int i = 0; i < arr.Length - 1; i++)
{
demo[i] = arr[i + 1];
}
demo[demo.Length - 1] = arr[0];
return demo;
}
public int[] shiftRight(int[] arr)
{
int[] demo = new int[arr.Length];
for (int i = 1; i < arr.Length; i++)
{
demo[i] = arr[i - 1];
}
demo[0] = arr[demo.Length - 1];
return demo;
}
LINQ solution, just to add some diversity.
static int[] LeftShift(int[] array)
{
// all elements except for the first one... and at the end, the first one. to array.
return array.Skip(1).Concat(array.Take(1)).ToArray();
}
static int[] RightShift(int[] array)
{
// the last element (because we're skipping all but one)... then all but the last one.
return array.Skip(array.Length - 1).Concat(array.Take(array.Length - 1)).ToArray();
}
Probably not recommended if performance matters (for large arrays).
I realize that the OP is not supposed to use a "built-in function".
public static int[] shiftRight(int[] arr){
int[] demo = new int[arr.Length];
for (int i = 0; i <= arr.Length - 2; i++)
{
demo[i + 1] = arr[i];
}
demo[0] = arr[arr.Length - 1];
return demo;
}
The problem here is that you need to special case the left shift of the first element. For every element but the first the new index of the value will be oldIndex - 1. This is essentially what the loop is doing. However the first element has a new index of oldLength - 1. This needs to be special cased somewhere in the code base.
Try this,
public int[] ShiftRight(int[] arr)
{
int[] demo = new int[arr.Length];
for (int i = 0; i < arr.Length; i++) {
demo[i] = arr[i == 0 ? (arr.Length - 1) : (i - 1)];
}
return demo;
}
Use Arra.Copy...
public int[] shiftLeft(int[] arr) {
var result = new int[arr.Length];
Array.Copy(arr, 1, result, 0, arr.Length - 1);
result[arr.Length - 1] = arr[0];
return result;
}
public int[] shiftRight(int[] arr) {
var result = new int[arr.Length];
Array.Copy(arr, 0, result, 1, arr.Length - 1);
result[0] = arr[arr.Length - 1];
return result;
}
Usually I use this code. You can rewrites to the array extension method.
public static T[] Shift<T>(T[] array, int shiftValue)
{
var newArray = new T[array.Length];
shiftValue -= array.Length;
if(shiftValue < 0)
{
shiftValue*=-1;
}
for(var i=0; i<array.Length; i++)
{
var index = (i + shiftValue) % array.Length;
newArray[i] = array[index];
}
return newArray;
}
maybe this works for anyone seeing this post :
private int[] shiftLinear(int[] linArray, int shift){
int length = linArray.Length;
int[] shifted = new int[length];
shift = shift % length;
if (shift >= 0) {
for (int n = shift ; n < length; n++) shifted[n] = linArray[n-shift];
if (shift != 0) for (int n = 0; n < shift; n++) shifted[n] = linArray[length-1-n];
} else {
for (int n = 0 ; n < length+shift; n++) shifted[n] = linArray[n-shift];
for (int n = length+shift; n < length ; n++) shifted[n] = linArray[n-length-shift];
}
return shifted;
}
Without using external array;
public static int[] right(int[] A)
{
var tempo = A[0];
for(var i=0; i<A.Length-1; i++)
{
var yolo = A[i + 1];
A[i + 1] = tempo;
tempo = yolo;
}
A[0] = tempo;
return A;
}
public static int[] left(int[] A)
{
var tempo = A[A.Length - 1];
for (var i = A.Length - 1; i >0; i--)
{
var yolo = A[i - 1];
A[i -1] = tempo;
tempo = yolo;
}
A[A.Length - 1] = tempo;
return A;
}
public int[] shiftRight(int[] arr)
{
int[] demo = new int[arr.length];
Array.Copy(arr,arr.Length-1,demo,0,1); // Copy last position to first
Array.Copy(arr,0,demo,1,arr.Length-1); // Copy the rest shifted one
return demo;
}