algorithm

Google Interview: Find all contiguous subsequence in a given array of integers, whose sum falls in the given range. Can we do better than O(n^2)?

主宰稳场 提交于 2021-02-05 12:42:11
问题 Given an array of Integers, and a range (low, high), find all contiguous subsequence in the array which have sum in the range. Is there a solution better than O(n^2)? I tried a lot but couldn't find a solution that does better than O(n^2). Please help me find a better solution or confirm that this is the best we can do. This is what I have right now, I'm assuming the range to be defined as [lo, hi] . public static int numOfCombinations(final int[] data, final int lo, final int hi, int beg,

How to calculate percentage between the range of two values a third value is

一个人想着一个人 提交于 2021-02-05 12:39:15
问题 Example: I'm trying to figure out the calculation for finding the percentage between two values that a third value is. Example: The range is 46 to 195. The value 46 would 0%, and the value 195 would be 100% of the range. What percentage of this range is the value 65? rangeMin=46 rangeMax=195 inputValue=65 inputPercentage = ? 回答1: Well, I would use the formula ((input - min) * 100) / (max - min) For your example it would be ((65 - 46) * 100) / (195 - 46) = 12.75 Or a little bit longer range =

How to sort an array efficiently

♀尐吖头ヾ 提交于 2021-02-05 11:29:05
问题 I'm trying to sort an array [3,3,2,1,3,2,2,2,1] to [1,1,3,3,3,2,2,2,2] . I'm trying to handle it using object, using the number as key, and the occurrence as value. const sortNums = (arr) => { const result = {} for (let i = 0; i < arr.length; i++) { const num = result[arr[i]] || 0; result[arr[i]] = num + 1; } //The above will gives me { '1': 2, '2': 4, '3': 3 } //How to convert back to array? } console.log(sortNums([3,3,2,1,3,2,2,2,1])) Of course I can use the Object.entries to map back to an

How to sort an array efficiently

半城伤御伤魂 提交于 2021-02-05 11:28:27
问题 I'm trying to sort an array [3,3,2,1,3,2,2,2,1] to [1,1,3,3,3,2,2,2,2] . I'm trying to handle it using object, using the number as key, and the occurrence as value. const sortNums = (arr) => { const result = {} for (let i = 0; i < arr.length; i++) { const num = result[arr[i]] || 0; result[arr[i]] = num + 1; } //The above will gives me { '1': 2, '2': 4, '3': 3 } //How to convert back to array? } console.log(sortNums([3,3,2,1,3,2,2,2,1])) Of course I can use the Object.entries to map back to an

Algorithm or formula that can take an incrementing counter and make it appear uniquely random

百般思念 提交于 2021-02-05 11:16:09
问题 I am wondering if there is a general formula of some sort that can take a single incrementing integer, and run it through a modulus sort of thing to shift it to a random place, so as you increment the counter, its output value jumps around and appears random, yet no value is ever hit twice. Assuming some limit on the set of numbers like 16-bit integers (65536 integers), or 32-bit integers, etc.. Perhaps there is a way to spiral numbers down somehow, I don't know. The sequence would be

What is the difference between tree depth and diameter?

陌路散爱 提交于 2021-02-05 11:12:44
问题 Hi Im little confused with the difference between the depth and the diameter of a tree.Sorry if Its already asked but I couldn't find it. 回答1: The depth of a node is the number of edges from the node to the tree's root node. A root node will have a depth of 0. The height of a node is the number of edges on the longest path from the node to a leaf. A leaf node will have a height of 0. The diameter (or width ) of a tree is the number of nodes on the longest path between any two leaf nodes. The

Why we can use sqrt(n) instead of n/2 as an upper bound while finding prime numbers? [duplicate]

纵然是瞬间 提交于 2021-02-05 09:29:06
问题 This question already has answers here : Why do we check up to the square root of a prime number to determine if it is prime? (13 answers) Closed 7 years ago . How we can use sqrt(n) instead of n/2 in this code? Is it correct to use sqrt(n) ? static boolean isPrime(long n) { if(n<=1) return false; double limit = Math.sqrt(n); for(long i = 2; i <= limit; i++) { if(n%i==0) return false; } return true; } 回答1: if n is not a prime, say n = p * q , then p and q cannot be both greater than sqrt(n)

Why we can use sqrt(n) instead of n/2 as an upper bound while finding prime numbers? [duplicate]

谁都会走 提交于 2021-02-05 09:28:10
问题 This question already has answers here : Why do we check up to the square root of a prime number to determine if it is prime? (13 answers) Closed 7 years ago . How we can use sqrt(n) instead of n/2 in this code? Is it correct to use sqrt(n) ? static boolean isPrime(long n) { if(n<=1) return false; double limit = Math.sqrt(n); for(long i = 2; i <= limit; i++) { if(n%i==0) return false; } return true; } 回答1: if n is not a prime, say n = p * q , then p and q cannot be both greater than sqrt(n)

How does while loop work without a condition?

旧街凉风 提交于 2021-02-05 09:17:45
问题 I understand that it's removing the first 3 elements of the array and adding them to the new array. But how does the function continue to add ensuing chunks of the array to the new array variable? How does the while loop work without proper conditions? How does it work in collaboration with splice() here? function chunkArrayInGroups(arr, size){ let newArr = []; while(arr.length){ newArr.push(arr.splice(0, size)) } return newArr; } chunkArrayInGroups(["a", "b", "c", "d"], 2); 回答1: The

Get a set of 2d list in python

吃可爱长大的小学妹 提交于 2021-02-05 08:54:05
问题 I have a list like follows t=[[1, 7], [3, 7], [1, 7], [5, 8], [3, 7]] I need to get a set out of this so the output would be like t=[[1, 7], [3, 7], [5, 8]] I tried to use t=set(t) but it didn't work 回答1: If you do not care about the order, you can first convert the inner lists to tuples using map() function, and then convert them to set and then back to list . Example - >>> t=[[1, 7], [3, 7], [1, 7], [5, 8], [3, 7]] >>> t = list(set(map(tuple,t))) >>> t [(3, 7), (5, 8), (1, 7)] 回答2: The