I been practicing my algorithm solving skill by solving HackerRank problems. However, one of the problems is giving me a difficult time to understand. It is about calculating absolute difference between sum of diagonals in a square matrix “arr”. If the square matrix is like this: and the solution is like this: So, I came up with this solution: However,
Tag: algorithm
How to check if any array members can be sum up to the largest of them in JavaScript?
The goal is to create a function that takes an array of numbers as a parameter and checks if the largest of them can be obtained as the sum of any of the other numbers in the array. One condition is that negative numbers can be a part of the array taken as a parameter. The problem The function I
How to insert an array to another array at each iteration of a for loop in javascript
I have a function to bubble sort and I want to save the array after each swap into another array. The bubble sort is working properly and I can log the array after each swap to the console. But I cant seem to push to the other array properly. Here’s my code : Here’s a screenshot of the console :
Merge sort time complexity check
Hello guys I was learning about the merge sort so I wrote this function that takes 2 arrays and merges them sorted. Can someone tell me the time complexity of this function? I thought it would be O(n^2) as I am using shift inside a while loop. Is that correct or am I missing something here? Answer The worst case
Javascript Building a Nested Object (tree) from an very weird array Debugging
I could really use some help Debugging a problem. Im trying to build a nested object (tree) from a very unusual array im getting from a backend (dont have access to the BE so i have to work with what i got). I’ve gotten 95 percent of it done but im running into a bug I know there are many
How to combine the values which is array of the same property in the object’s array javascript
For the following array : I need a function that should merge arrays with the same keys in the object. Answer Try to use reduce
Finding a string of length 6 in a 6^26 array of strings [closed]
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 8 months ago. Improve this question I have a task to create a JS script that is able to find a string using binary search on
Return sequence with elements from array A except those that are present in B p times
I want to write a function that receives two sequences: A and B and returns sequence C which should contain all elements from A (in order) except those that are present in B p times. For example sequences A=[2,3,9,2,5,1,3,7,10] B=[2,1,3,4,3,10,6,6,1,7,10,10,10] Should return C=[2,9,2,5,7,10] When p = 2 I wrote it like this: But is there a better way to make
The elegant way to resolve negative zero in Javascript
I have to multiply the sign of all elements in an array. For example 1: Ex2: Ex3: And here is my solution However, the output of ex3 cal([1, -2, 3, 0]) is -0. I’ve already thought about adding one more condition like this And obviously, It looks ugly. Is there a more elegant way to resolve that? Answer In order
JavaScript array Sort out! Indices problem
Given an array A of non-negative integers of size m. Your task is to sort the array in non-decreasing order and print out the original indices of the new sorted array. e.g.A={4,5,3,7,1} After sorting the new array becomes A={1,3,4,5,7}. The required output should be “4 2 0 1 3” Answer You need to pair (tuple) the value with the original