Hot answers tagged

9

Answering your first question, yes there is a better way. Efficiency doesn't matter in your case but in this instance the more efficient thing is also easier on the eyes. The usual idea would be to make a hash set of the elements, then you have O(1) look up of whether you have already inserted an elements, making the task O(n^2). I'm not that familiar with ...


5

Just few small points. You can use std::array. You can have the size represent half of the entire diameter. Thus having nArray<T, 50> allocate memory for indices -50 to 50 and avoid the odd size check. You might also reconsider if you really need [][] access and whether it wouldnt be better to implement a class that has just 1d array to represent 2d ...


4

I think considering approach that you chose, your code is alright. A few points: What I don't like is that you get error when passing 2 empty arrays? Why would you do that? Imagine I am generating arrays of different sizes and using your code to merge and sometimes I would pass 2 empty arrays. Should I really need to handle that as special case in my code? ...


4

Note, using Sets, is the way to go. My answer is based upon the assumption that you didn't learn about Sets and you can only use arrays. duplicated values A1 A2 A3 A4 A5 A6 A7 A8 A9 your loop: for (int i = 0; i < length; i++){ for (int j = 0; j < length; j++){ int x = square[i][j]; for (int a = 0; a < length; a++){ ...


4

The "odd" check in the constructor is a bit obfuscated, and it does not need to be executed at runtime. A simpler static_assert to check for odd (and non-negative) would report errors sooner. std::static_assert(SIZE >= 0 && (SIZE & 1) == 1); Having operator[] perform a range check is atypical. The operator[] is typically fast, with no ...


3

const mergeSortedArrays = (arr1, arr2) => [...arr1, ...arr2].sort((a,b) => a-b); const ar1 =[-7, 2, 4, 22, 66, 99]; const ar2 = [1, 5, 9, 88]; console.log( mergeSortedArrays(ar1, ar2) ); // [ -7, 1, 2, 4, 5, 9, 22, 66, 88, 99 ] I don't know if it's not too simple solution, but one-line Arrow function solves this problem... OR in your code instead ...


3

The first comment has to do with your question about Results. IMO you are far better off to implement your ArrayToX and XToArray subroutines as functions. Also, I tried to use your module (Class Module or Standard Module? => recommend ClassModule) and had difficulty understanding how to use the Filters. In fact, I never did figure it out. I wrote a test ...


2

Based on your constraints, which are: Single loop variable No restarting of iteration over large arrays You could calculate your looping step sizes first, then go over the array only once. const mushedArr = []; const remainderSize = arr.length % size const numberOfChunks = Math.floor(arr.length / size); let remainderStepSize = Math.floor(...


2

What do you mean optimal? Your thing transforms the object lots of times, which is liable to not be very efficient, if that's what you meant. Here's something that I hope is simple to read that does your task. function sumObjects(obj1, obj2) { return Object.keys(obj2).reduce( (acc, currKey) => ({ ...acc, [currKey]: acc[...


2

I'll have a go at your first question, using a Set: Set<Integer> seenNumbers = new HashSet<Integer>(); for (int i = 0; i < length; ++i) { for (int j = 0; j < length; ++j) { if (!seenNumbers.add(square[i][j])) { return false; } } } ... rest of method It keeps adding elements to the Set until it finds a ...


1

I think the nicest thing to do is to split your task into three bits, essentially you want to see if a row, column or diagonal has all the entries the same. A row, column or diagonal can all be thought of just as a (1D) array. In fact, these things are all the 'lines' (pictorially) that you can find in a 2D array (which span the whole array). Now given a ...


1

For solution (a) - just accept a shorter array at the end code can be very short function mushInLittleArray(arr, size) { let resultArr = []; for (let i = 0; i < (arr.length / size); i++ ) { resultArr.push( arr.slice( i * size, (i+1) * size ) ); } return resultArr; }


1

My java is a bit rusty, so my answer is going to be more generic than just java. For 1., as @tieskedh points out, you want to use sets. If you don't have to available, you can emulate one by creating a list containing all the elements, sorting it and checking that none of the values has a neighbour that is identical to itself. This will be effecient and ...


1

Just one remark: do not copy-paste code and then change one tiny bit of it. This indicates that you should create a method. What I mean is this: var newPoint = new CustomPoint(pointToProcess.Column, pointToProcess.Row + 1); grid[newPoint.Row, newPoint.Column] = 1; toPropagateInNextGeneration.Enqueue(newPoint); Those three lines are always the same, except ...


1

Algorithm in O(N^2) The current solution will be slow for large N, because it has a complexity of O(N^2) (It checks every element in the array for every possible position of the k adjusted elements => O(N * (N-K)) => O(N^2)). There is an O(N) solution. Consider that as the K-element segment "slides" along the array, there are only two elements whose value ...


1

You can speed some things up by using more list and dict comprehensions, and by reducing your function calls. Some examples follow. d = dict() vs d = {}: 131 ns vs 30 ns. len_of_half_a = int(len(a)/2) vs len_of_half_a = len(a)//2: 201 ns vs 99 ns. I used Python 3.8.1 for both tests. Granted that this isn't much, but several of these tiny improvements ...


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