Tag Info

10

See this Stackoverflow answer for a discussion on the optimal growth factor for dynamic arrays. The gist of it is that it depends, but growth factors around the golden ratio are easier for the memory allocator to handle. I'd recommend using 1.5 because it is easy to implement: DEST->cap += DEST->cap / 2 It likely fares worse on artifical benchmarks ...

7

We're missing an include of <stddef.h> to define size_t (or any of the other headers which define it). We're missing an include of <stdlib.h>, needed for malloc() and friends (and this would define size_t for us, too). With those fixed, and sufficient minor changes to the test program, I managed to compile with only a few warnings. DEST = ...

7

How about an alternate approach: instead of adapting the binary-search algorithm to work with all kinds of alternate sorted collections, how about adapting the collections to be able to run classic binary search on them. This enables one to use production ready searching methods without reinventing the wheel and risking bugs. Even if one decides to rewrite ...

5

here are some suggestions for your code. 1) Since the array is not updated, I suggest that you extract the size in a variable. public static int find(int[] a, int x) { //[...] int arrLength = a.length; //[...] } 2) In my opinion, there's no gain to check if the boundaries are the current value, since the value can be anywhere. else if (a[0] ==...

4

How can I decrease code length? You could split your big for loop into 2 smaller ones. The first one would print all diagonals up to the main one, and the other one the remaining ones. For instance, let's say there's a matrix like: 1 2 3 4 5 6 7 8 9 Then, your first loop would print 1 4 2 7 5 3 and the second one 8 6 9. The code would look conciser and ...

4

Your function does a linear search over all elements of the nested array, until the given number is found, or all elements have been visited. The complexity is $O(mn)$ for an $m$-by-$n$ matrix. For a square $n$-by-$n$ matrix that makes $O(n^2)$, and not $O(n)$ as you claimed. This can be improved, but let's first do a Review of ...

3

It seems you do not need those lines: } else if (a[0] == x) { return 0; } else if (a[topOdd] == x) { return topOdd;

3

I find the big points covered well: class and methods have one clear, documented purpose the API follows the well-known java.util.Arrays (if not to the point of documenting RuntimeExceptions thrown) I'd try to get rid of magic literals and code replication: size counts = new int[Byte.MAX_VALUE-Byte.MIN_VALUE+1] (or 1<<Byte.SIZE?), use for (int ...

3

Since, there is an array of values, no need to iterate in the range, I think. Why not iterating between the array elements? Dim arr As Variant, sh As Worksheet, El As Variant Dim refVal As Variant, boolWrong As Boolean, strDif As String Set sh = ActiveSheet arr = Application.Index(sh.Range("A1:AA1").Value, 1, Array(1, 2, 3, 6, 11, 24, 27)) refVal = arr(1)...

3

Using a Dictionary to count the occurences of a single value is overkill. If the question was count the number of values that match the first then If FirstValue = cl.Value Then Count = Count + 1 would be far more efficient than using a Dictionary. I would like to know if all cells are equal to the first Cell in the Range It seems to me that you are over ...

2

It is possible to combine Application.Find with Application.Sum (arr2 in the code below) on the extracted array (arr in the code) to get the same result as Countif. The reason is that Application.Find returns an array filled with 1 if the value is found in the corresponding element (See picture below.) But Application.Find will return false positives if the ...

2

For such specific problems as you are facing here I almost always turn to BenchmarkDotNet, because it provide you with an easy what to try out different implementation strategies and various .NET features that might tweak the performance to your satisfaction. Parallel conversion Parallel.ForEach(...) and allocate a fair chunk of the array(s) to each of ...

2

array_replace() is the perfect call here -- it can be used to overwrite the master array with the array containing actual integer values. Code: (Demo) var_export(array_replace(array_map('intval', $all_states),$country_states)); To streamline the process futher, you should declare your master list with 0 values instead of null values, then you can omit ...

2

It seems this could be simplified with a small lookup table and then concatenating strings. Using a table as simple as: const lookup = { blue: 'b', green: 'f', purple: 'g', yellow: 'p' }; we can then lookup the color values and concatenate them together. Combined with a simple check to make sure the color is valid and the 2 colors aren't the same, ...

2

Firstly, main() must return an int: int main(void) We can make the array initialization easier to read with judicious use of whitespace: static const int N = 5; const int pixel_array[5][5] = { {1, 3, 6, 10, 15}, {2, 5, 9, 14, 19}, {4, 8, 13, 18, 22}, {7, 12, 17, 21, 24}, {11, 16, 20, 23, 25} }; Instead of the if/...

2

The minor problem is that you are printing the result in the wrong place. The major problem is that n may be up to 100,000. This means you need to run 10,000,000,000 of your inner test. And you may have only 0.02 seconds to do that. The trick to dealing with that is probably to determine the maximum possible score of a given i and l[i], and skip the ...

1

solve() shouldn't print anything, much less all the intermediate results. It should end with return ans and the main program should print that final result. You'll get the same answer for (i,j) as for (j,i), so for the inner loop j never needs to be less than i. The expression abs(l[i] - l[j]) + abs(i - j) gets calculated twice. Instead, assign the value ...

1

unsigned vs, unsigned long Code mixes use of unsigned and unsigned long as if they are of the 32-bit size. In 2020, 16-bit unsigned is commonly found in embedded processors and 64-bit unsigned long in 64-bit processors. Do not assume unsigned, unsigned long size/range other than they are at least 16, 32 bits. #define UINT_MAXIMUM 4294967294 // UINT_MAX ...

1

It's been a long time since I've heard the terms BCD or Binary Coded Decimal. Keep in mind that BCD was primarily a hardware encoding of integers provided by one particular manufacturer of main frame computers. There were computer performance issues when using BCD. The best way to implement a BCD number would be an array of 4 bit integer values. One good ...

1

I think I found a solution that seemed the most elegant: Sub Test() 'Get a 1D-array from columns of interest Dim arr As Variant: arr = Application.Index(Range("A1:AA1").Value, 1, Array(1, 2, 3, 6, 11, 24, 27)) 'Check if all elements in array match the first element With Application If .Count(.Match(arr, Array(arr(1)), 0)) = 7 Then Debug.Print "...

Only top voted, non community-wiki answers of a minimum length are eligible