I have a 1-dimensional array that fills up a table of 40 random elements (all the values are either 0 or 1). I want to find the longest consecutive span of values.
For example:
In 111100101 the longest row would be 1111 because it has four consecutive values of 1.
In 011100 the result is 111.
I have no idea how to check upon the "next element" and check if it's a 0 or 1.
Like the first would be 1111 (count 4) but the next would be a 0 value, meaning I have to stop counting.
My idea was placing this value (4) in a other array (example: 111100101), and place the value of the 1's back on zero. And start the process all over again.
To find the largest value I have made another method that checks up the biggest value in the array that keeps track of the count of 0's 1's, this is not the problem.
But I cannot find a way to fill the array tabelLdr up, having all the values of the group of elements of the same kind (being 0 or 1).
In the code below I have 2 if's and of course it will never go into the second if (to check if the next value in the array is != to its current state (being 0 or 1).
public void BerekenDeelrij(byte[] tabel, byte[] tabelLdr)
{
byte LdrNul = 0, Ldréén = 0;
//byte teller = 0;
for (byte i = 0; i < tabel.Length; i++)
{
if (tabel[i] == 0)
{
LdrNul++;
//this 2nd if cleary does not work, but i have no idea how to implend this sort of idea in my program.
if (tabel[i] == 1) //if value != 0 then the total value gets put in the second array tabelLdr,
{
tabelLdr[i] = LdrNul;
LdrNul = 0
}
}
if (tabel[i] == 1)
{
Ldréén++;
if (tabel[i] == 0)
{
tabelLdr[i] = Ldréén;
Ldréén = 0;
}
}
}/*for*/
}
This method should do what you need:
public int LargestSequence(byte[] array) {
byte? last = null;
int count = 0;
int largest = 0;
foreach (byte b in array) {
if (last == b)
++count;
else {
largest = Math.Max(largest, count);
last = b;
count = 1;
}
}
return Math.Max(largest, count);
}
Even while i is the loop counter, it is still just a variable. A valid for statement is for (;;), which is an infinite loop. Notice the for statement increments i, as in i++. The expression i = i + 1 works just as well.
Im unsure if you need the longest "row" of ones or longest row of either 0 or 1. This will work for the latter
var max = 0;
var start = 0;
var current = -1;
var count = 0;
for(int i = 0;i<table.Length;i++)
{
if(current = table[i])
{
count++;
}
else
{
current = table[i];
if(max < count)
{
max = count;
start = i-count;
}
count = 1;
}
}
if(max < count)
{
max = count;
start = i-count;
}
//max is the length of the row starting at start;
Related
I solved a task on Hackerrank.com, where the problem was like this:
You have an Array. This Array contains numbers.
Now you enter two numbers:
The first one describes a sum
The second one describes the amount of indexes (sequence length) you add together
In the end you get the amount of sequences whose sum is your defined number
For example:
Your array is [ 1, 2, 3, 4], your sum is 3 and your sequence length is 2.
Now you take the first two indexes and output the sum: [1, 2] = 3.
This is equal to your sum, so now you have found one sequence.
The next sequence is [ 2, 3 ] = 5. This is not equal to 3, so your sequence counter stays 1.
The last sequence is [3, 4] = 7. This is also not equal to 3 and in the end, you found one sequence.
I wrote this code for that:
static int GetSequences(List<int> s, int d, int m)
{
//m = segment-length
//d = sum
int count = 0;
int j = 0;
int k = 0;
do
{
try
{
List<int> temp = new List<int>();
for (int i = 0; i < m; i++)
{
temp.Add(s[i + k]);
}
if (temp.Sum() == d)
{
count++;
}
j++;
k++;
}
catch (ArgumentOutOfRangeException)
{
break;
}
} while (true);
return count;
}
As I didn't know how often I have to count
(For example a 6-Length-Array with a sequence-length of 3 has 4 sequences (1,2,3 | 2,3,4 | 3,4,5 | 4,5,6)),
I am stopping the while loop when the index is out of range. but I'm not sure if this solution is okay. Not just with program speed, but also with code cleanliness. Is this code acceptable, or is it better to use a for loop, which loops for example exactly 4 times for a 6-length array with 3-Length sequences?
It's not recommended, no. Exceptions should be reserved for stuff that isn't supposed to happen, not flow control or validation.
What you want is to use conditional logic (if statements) and the break keyword.
Also, codereview.stackexchange.com is better suited for these kinds of questions.
It would be better to fix your code so that it doesn't routinely throw exceptions:
You sum each of these segments:
0 1 2 3 start = 0
| | summing indexes: 0, 1
+--+
0 1 2 3 start = 1
| | summing indexes: 1, 2
+--+
0 1 2 3 start = 2
| | summing indexes: 2, 3
+--+
The bracket starts at the index start, and has a size of m. The length of s is given by s.Count. Therefore we want to keep going until start + m == s.Count.
(I always find it's useful to draw these things out, and put sample numbers in, in order to make sure you've got the maths right. In the sample above, you can see that we stop when start (2) + m (2) == the array size (4))
static int GetSequences(List<int> s, int d, int m)
{
//m = segment-length
//d = sum
int count = 0;
for (int start = 0; start + m <= s.Count; start++)
{
List<int> temp = new List<int>();
for (int i = 0; i < m; i++)
{
temp.Add(s[start + i]);
}
if (temp.Sum() == d)
{
count++;
}
}
return count;
}
However, you can improve your code a bit:
Use meaningful variable names
Don't create a new temporary list each time, just to sum it
Check your inputs
static int GetSequences(List<int> numbers, int targetSum, int segmentLength)
{
if (numbers == null)
throw new ArgumentNullException(nameof(numbers));
if (segmentLength > numbers.Count)
throw new ArgumentException("segmentLength must be <= numbers.Count");
int count = 0;
for (int start = 0; start + segmentLength <= numbers.Count; start++)
{
int sum = 0;
for (int i = 0; i < segmentLength; i++)
{
sum += numbers[start + i];
}
if (sum == targetSum)
{
count++;
}
}
}
Usually except for switch/case there is often no real reason to use break.
Also an exception MUST be as the name says exceptional, so it MUST NOT be a part of your logic.
As said Jeppe you can use the methods and attributes the framework provides you to do as you like.
Here s.Count seems to be the way to go.
int[] arr = new[] { 1, 2, 1, 2 };
// Sum and len are given by the task.
// 'last' is the last index where we should stop iterating.
int sum = 3, len = 2, last = arr.Length - len;
// One of the overloads of Where accepts index, i.e. the position of element.
// 1) We check that we don't go after our stop-index (last).
// 2) Avoid exception by using '&&'.
// 3) We use C# 8 Range (..) to get the slice of the numbers we need:
// we start from the current position (index) till then index,
// calculated as current index + length given by the task.
// 4) Sum all the numbers in the slice (Sum()) and compare it with the target sum,
// given by the task (== sum).
// 5) The count of all matches (Count()) is the sought amount of sequences.
int count = arr.Where((z, index) => index <= last && arr[index..(index+len)].Sum() == sum).Count();
I am having trouble with a small bit of code, which in a random size array, with random number pairs, except one which has no pair.
I need to find that number which has no pair.
arLength is the length of the array.
but i am having trouble actually matching the pairs, and finding the one which has no pair..
for (int i = 0; i <= arLength; i++)
{ // go through the array one by one..
var number = nArray[i];
// now search through the array for a match.
for (int e = 0; e <= arLength; e++)
{
if (e != i)
{
}
}
}
I have also tried this :
var findValue = nArray.Distinct();
I have searched around, but so far, i haven't been able to find a method for this.
This code is what generates the array, but this question isn't about this part of the code, only for clarity.
Random num = new Random();
int check = CheckIfOdd(num.Next(1, 1000000));
int counter = 1;
while (check <= 0)
{
if (check % 2 == 0)
{
check = CheckIfOdd(num.Next(1, 1000000)); ;
}
counter++;
}
int[] nArray = new int[check];
int arLength = 0;
//generate arrays with pairs of numbers, and one number which does not pair.
for (int i = 0; i < check; i++)
{
arLength = nArray.Length;
if (arLength == i + 1)
{
nArray[i] = i + 1;
}
else
{
nArray[i] = i;
nArray[i + 1] = i;
}
i++;
}
You can do it using the bitwise operator ^, and the complexity is O(n).
Theory
operator ^ aka xor has the following table:
So suppose you have only one number without pair, all the pairs will get simplified because they are the same.
var element = nArray[0];
for(int i = 1; i < arLength; i++)
{
element = element ^ nArray[i];
}
at the end, the variable element will be that number without pair.
Distict will give you back the array with distinct values. it will not find the value you need.
You can GroupBy and choose the values with Count modulo 2 equals 1.
var noPairs = nArray.GroupBy(i => i)
.Where(g => g.Count() % 2 == 1)
.Select(g=> g.Key);
You can use a dictionary to store the number of occurrences of each value in the array. To find the value without pairs, look for a (single) number of occurrences smaller than 2.
using System.Linq;
int[] data = new[] {1, 2, 3, 4, 5, 3, 2, 4, 1};
// key is the number, value is its count
var numberCounts = new Dictionary<int, int>();
foreach (var number in data) {
if (numberCounts.ContainsKey(number)) {
numberCounts[number]++;
}
else {
numberCounts.Add(number, 1);
}
}
var noPair = numberCounts.Single(kvp => kvp.Value < 2);
Console.WriteLine(noPair.Key);
Time complexity is O(n) because you traverse the array only a single time and then traverse the dictionary a single time. The same dictionary can also be used to find triplets etc.
.NET Fiddle
An easy and fast way to do this is with a Frequency Table. Keep a dictionary with as key your number and as value the number of times you found it. This way you only have to run through your array once.
Your example should work too with some changes. It will be a lot slower if you have a big array.
for (int i = 0; i <= arLength; i++)
{
bool hasMatch = false;
for (int e = 0; e <= arLength; e++)
{
if (nArray[e] == nArray[i])//Compare the element, not the index.
{
hasMatch = true;
}
}
//if hasMatch == false, you found your item.
}
All you have to do is to Xor all the numbers:
int result = nArray.Aggregate((s, a) => s ^ a);
all items which has pair will cancel out: a ^ a == 0 and you'll have the distinc item: 0 ^ 0 ^ ...^ 0 ^ distinct ^ 0 ^ ... ^0 == distinct
Because you mentioned you like short and simple in a comment, how about getting rid of most of your other code as well?
var total = new Random().Next(500000) * 2 + 1;
var myArray = new int[total];
for (var i = 1; i < total; i+=2)
{
myArray[i] = i;
myArray[i -1] = i;
}
myArray[total - 1] = total;
Then indeed use Linq to get what you are looking for. Here is a slight variation, returning the key of the item in your array:
var key = myArray.GroupBy(t => t).FirstOrDefault(g=>g.Count()==1)?.Key;
int[,] data = new int[,] {
{ 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0 },
{ 0, 1, 1, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 },
};
int[,] find = new int[,] {
{ 0, 1 },
{ 1, 1 }
};
bool result = Check2DArray (data, find);
How do I search for a small 2D array in a large 2D array?
You can try this snippet :
static bool Check2DArray(int[,] data, int[,] find)
{
int dataLen = data.Length; // length of the whole data
int findLen = find.Length; // length of the whole find
for(int i = 0; i < dataLen; i++) // iterate through data
{
int dataX = i % data.GetLength(0); // get current column index
int dataY = i / data.GetLength(0); // get current row index
bool okay = true; // declare result placeholder for that check
for (int j = 0; j < findLen && okay; j++) // iterate through find
{
int findX = j % find.GetLength(1); // current column in find
int findY = j / find.GetLength(1); // current row in find
int checkedX = findX + dataX; // column index in data to check
int checkedY = findY + dataY; // row index in data to check
// check if checked index is not going outside of the data boundries
if ( checkedX >= data.GetLength(0) || checkedY >= data.GetLength(1))
{
// we are outside of the data boundries
// set flag to false and break checks for this data row and column
okay = false;
break;
}
// we are still inside of the data boundries so check if values matches
okay = data[dataY + findY, dataX + findX] == find[findY, findX]; // check if it matches
}
if(okay) // if all values from both fragments are equal
return true; // return true
}
return false;
}
You can check that online here
Please give me some feedback and if it is not clear enough I can explain in more details :)
EDIT :
Found an issue that this method was not checking last row and column in the data argument. Now after small fix all works perfectly.
EDIT2 :
Thanks to #SefaTunçkanat for pointing out my mistake. There was an issue with some calculations which could lead to IndexOutOfRange exceptions and comparing wrong inputs. Now everything works fine.
Make a helper method that checks if a big array contains a small array starting at the given position (row, col). Make two nested loops to iterate over all (row, col) pairs in the big array where the small array could fit, and see if any pair would produce a match.
static bool EqualAtPosition(int[,] big, int[,] small, int row, int col) {
var rowCount = small.GetLength(0);
var colCount = small.GetLength(1);
if (row+rowCount > big.GetLength(0) || col+colCount > big.GetLength(1)) {
return false;
}
for (var r = 0 ; r != rowCount ; r++) {
for (var c = 0 ; c != colCount ; c++) {
if (big[row+r, col+c] != small[r, c]) {
return false;
}
}
}
return true;
}
Basically you just loop through all "start" positions that the smaller array could fit in the bigger one, then loop through the values of the smaller array and compare to the relative position in the bigger, if there isn't a match you need to continue to the next position in the bigger array, or if all match you can return true, if you finish going through the bigger array without finding a match then you return false.
public static bool Check2DArray(int[,] data, int[,] find)
{
for (int dRow = 0; dRow < data.GetLength(0) - find.GetLength(0); dRow++)
{
for (int dCol = 0; dCol < data.GetLength(1) - find.GetLength(1); dCol++)
{
bool found = true;
for (int fRow = 0; fRow < find.GetLength(0); fRow++)
{
for (int fCol = 0; fCol < find.GetLength(1); fCol++)
{
if (data[dRow + fRow, dCol + fCol] != find[fRow,fCol])
{
found = false;
break;
}
}
if (!found) break;
}
if (found) return true;
}
}
return false;
}
Also you could use #dasblinkenlight's solution to replace the inner two loops if you want.
Depending on the size of the arrays and your requirements a simple brute-force algorithm may suffice.
You would start at [0,0] in your big array and check if all the items from this coordinate on are equal to the items in the small array.
If they are you have found a match. If not you would go to the next position in the big array.
take the first element of the small array at [0,0] (let's call it start_e)
run with 2 for-loops through the big array
if you find start_e copy the sub array from the big one of the size of the small one and
compare it with a helping method that can check 2 Arrays of same size.
The program starts by gathering the amount of allied/enemy minions in the game.
var eminions = MinionManager.GetMinions(Player.ServerPosition, 1500 MinionTypes.All, MinionTeam.Enemy).ToList();
var aminions = MinionManager.GetMinions(Player.ServerPosition, 1500, MinionTypes.All, MinionTeam.Ally).ToList();
Then i use a function that translates each object found in the lists into distances:
emd.Add(eminions[0].ServerPosition.Distance(aminions[i].ServerPosition)); //the count is 5
Our list 'emd' contains (aminions.Count) of distances which in our example = 5.
emd[0] = 500
emd[1] = 400
emd[2] = 300
emd[3] = 200
emd[5] = 100
Here's where it get's tricky, we want to find out where the smallest distance is located in the list. If we theorize we can see it's emd[5] = 100. But how do i get '5'?
Summary: The program must find the smallest number in the list, and then get the position in the list where it found it.
Here's what i tried: I tried comparing each number to see what's the smallest, if true, add to a new list, the problem im facing is that im creating too many lists and i am not prepared to handle varied amounts of minions.
var eminions = MinionManager.GetMinions(Player.ServerPosition, 1500, MinionTypes.All, MinionTeam.Enemy).ToList();
var aminions = MinionManager.GetMinions(Player.ServerPosition, 1500, MinionTypes.All, MinionTeam.Ally).ToList();
if (eminions.Count > 0 && aminions.Count > 0)
{
List<double> emd = new List<double>();
List<bool> bdistance = new List<bool>();
for (int i = 0; i < aminions.Count; i++)
{
emd.Add(eminions[0].ServerPosition.Distance(aminions[i].ServerPosition));
for (int j = 0; j < aminions.Count; j++)
{
if (emd[i] > emd[j])
{
bdistance.Add(true);
}
}
}
}
int pos = 0; // set index to zero
int min = emd[0]; // set minimum to first value
for (int i = 1; i < emd.Count; i ++)
{
var val = emd[i]; // get value from position
if (val < min) //check if value is smaller then current minimum
{
// set minimum to value
min = val;
// set minimum pos to current value pos
pos = i;
}
}
//found minimum
//minimum index == pos
You can find it easily!
var minIndex = emd.IndexOf(emd.Min());
This should work
int indexMin
= !emd.Any() ? -1 :
emd.Select( (value, index) => new { Value = value, Index = index } )
.Aggregate( (a, b) => (a.Value < b.Value) ? a : b )
.Index;
1.The !emd.Any() ? -1 : will force a -1 if the list is empty;
2.The Select will project each int element into an anonymous type with two properties: Value and Index;
3.The Aggregate will get the element with the minimum Value;
4.Finally, we get the Index of the chosen element.
I have an assignment where I need to find the product of all of the numbers in an array, I'm not sure how to do this.
int[] numbers = new int[SIZE];
Console.WriteLine("Type in 10 numbers");
Console.WriteLine("To stop, type in 0");
for (int input = 0; input < SIZE; input++)
{
userInput = Console.ReadLine();
numberInputed = int.Parse(userInput);
if (numberInputed == ZERO)
{
numberInputed = ONE;
break;
}
else
{
numbers[input] = numberInputed;
}
}
This is where I'm trying to find the product of all of the numbers in the array.
foreach (int value in numbers)
{
prod *= value;
}
Console.WriteLine("The product of the values you entered is {0}", prod);
What am I doing wrong in the foreach statement? Thanks in advance
Edit, left out my declared values
const int SIZE = 10;
const int ZERO = 0;
string userInput;
int numberInputed;
int prod = 1;
It now works when I type in all ten values but if I put a 0 in order to break the loop then everything equals 0. How do I prevent a 0 from being entered into the array?
It's possible you initialize prod to 0, which means no matter what numbers are in your array, prod will remain 0. Make sure you initialize it to 1 to get the correct result:
int prod = 1;
foreach (int value in numbers)
{
prod *= value;
}
You could also use Linq's Aggregate extension method to do the same thing:
using System.Linq; // put with other using directives
int prod = numbers.Aggregate(1, (a, b) => a * b);
Update
The real problem (which I failed to notice before) is that your array isn't being fully populated if you break out of your loop early. So any array entries you didn't set are still initialized to 0. To fix this, use a List<int> instead of an int[]:
using System.Collections.Generic; // put with other using directives
List<int> numbers = new List<int>(SIZE); // Capacity == SIZE
...
for (int input = 0; input < SIZE; input++)
{
...
if (numberInputed == ZERO)
{
break;
}
else
{
numbers.Add(numberInputed);
}
}
The problem is that you don't keep track of how many items there are in the array that actually are assigned a value. If you exit from the loop using a zero input, then the rest of the items are unchanged. As they are zero by default, you will be using those zeroes in your second loop, and when you have a zero somewhere in the array, the total product becomes zero.
Keep track of how many items there are by keeping the loop variable outside the loop:
int input = 0;
while (input < SIZE)
{
userInput = Console.ReadLine();
numberInputed = int.Parse(userInput);
if (numberInputed == ZERO) {
break;
}
numbers[input] = numberInputed;
input++;
}
Now you can use only the items that are actually assigned:
for (int i = 0; i < input; i++) {
prod *= numbers[i];
}
Multiply all numbers inside an Array
int[] array = { 1, 2, 3, 4, 5 };
int sum = array[0];
for (int i = 1; i != array.Length; i++)
{
sum *= array[i];
}
If your array is somehow populated with zeroes (0), then use List instead of an array.