algorithm

Algorithm to determine which numbers in a list are the sum of a given number

ぃ、小莉子 提交于 2021-02-07 11:09:41
问题 I have a coding question that I am try to solve. I haven't been able to come up with a good algorithm yet. Given a number X (e.g. 200), determine which numbers from a list (e.g.: 4,5,10, 10, 23,67,889, 150, 50) will when summed up will be equal to X. In this case the answer is (50, 150). So far I thought about first sorting the the list (lowest to highest) then loop through adding the numbers until I get to a value greater than X. Discard all the remaining numbers in the list since they are

Finding Diameter of a Tree

戏子无情 提交于 2021-02-07 10:51:41
问题 I have written code for finding the diameter of binary tree. But I am unable to figure out where is it going wrong . The two functions that I have written and their definition are as follows :- int btree::diameteroftree(node* leaf) { if (leaf==NULL) return 0; int lheight = hieghtoftree(leaf->left); int rheight = hieghtoftree(leaf->right); int ldiameter = diameteroftree(leaf->left); int rdiameter = diameteroftree(leaf->right); return max(lheight + rheight + 1,max(ldiameter,rdiameter)); } int

Pair with Maximum AND value

回眸只為那壹抹淺笑 提交于 2021-02-07 10:32:24
问题 Given a very large array of integers in which element can go upto 10^9 how do I find a pair with maximum AND value. My current approach is I calculate all possible pairs and traverse through it and find maximum, however it is very slow. Any other approach? 回答1: As long as you can find at least two numbers with the same most significant bit set, the solution will involve two of them. Next, discard all other elements and remove everything left of this MSB for the numbers not discarded and solve

Fastest way to find out a number with most divisors

只愿长相守 提交于 2021-02-07 10:28:10
问题 Given a number n how fast can one find the smallest number with most factors and is less than n? PS:other than the naive approach of finding number of divisors for all numbers upto n . UPDATE : My observation: int solve(int primes[],int s,int n) { int i=0; while(s<n) { s*=primes[i]; i++; } if(s>n) s/=primes[i-1]; return s; } int main() { int primes[] = {2,3,5,7,11,13,17,19,23,29,31,37}; int n; scanf("%d",&n); int s=1; while(s*2<n)//checking the possibility of existence of any prime such that

How to split continous attribute in CART decision tree algorithm?

拈花ヽ惹草 提交于 2021-02-07 10:21:55
问题 I don't understand about how to split continous attribute in CART (Classification and Regression Tree) algorithm, as we know that CART can both split categorical and continous attribute. i have read many papers and it says the value to be split point is the middle value in sequence. i don't understand about it. could you explain to me what that means, and give me some examples? thanks 回答1: The general process is to scan through candidate splitting values on any given predictor, measure the

Multiliteration implementation with inaccurate distance data

烂漫一生 提交于 2021-02-07 10:20:36
问题 I am trying to create an android smartphone application which uses Apples iBeacon technology to determine the current indoor location of itself. I already managed to get all available beacons and calculate the distance to them via the rssi signal. Currently I face the problem, that I am not able to find any library or implementation of an algorithm, which calculates the estimated location in 2D by using 3 (or more) distances of fixed points with the condition, that these distances are not

What's the worst-case valid sudoku puzzle for simple backtracking brute force algorithm?

那年仲夏 提交于 2021-02-07 09:48:59
问题 The " simple/naive backtracking brute force algorithm", "Straightforward Depth-First Search" for sudoku is commonly known and implemented. and no different implementation seems to exist. (when i first wrote this question.. i wanted to mean we could completely standardize it, but the wording is bad..) This guy has described the algorithm well i think: https://stackoverflow.com/a/2075498/3547717 Edit: So let me have it more specified with pseudo code... var field[9][9] set the givens in 'field'

A star algorithm: Distance heuristics

若如初见. 提交于 2021-02-07 09:35:33
问题 I am using the A star algorithm as seen here (taken from http://code.activestate.com/recipes/578919-python-a-pathfinding-with-binary-heap/), but I have a problem I don't understand. The heuristics given here is the square of the distance between two points. I found that if I take the square root of it instead, my results are more accurate, but the running time of the function increases dramatically (i.e. it goes over many, many more loops than before). Why does the change in heuristics cause

Wildcard String Search Algorithm

夙愿已清 提交于 2021-02-07 09:35:32
问题 In my program I need to search in a quite big string (~1 mb) for a relatively small substring (< 1 kb). The problem is the string contains simple wildcards in the sense of "a?c" which means I want to search for strings like "abc" or also "apc",... (I am only interested in the first occurence). Until now I use the trivial approach (here in pseudocode) algorithm "search", input: haystack(string), needle(string) for(i = 0, i < length(haystack), ++i) if(!CompareMemory(haystack+i,needle,length

Algorithms in O(n^2) vs O(n) [duplicate]

╄→尐↘猪︶ㄣ 提交于 2021-02-07 09:22:40
问题 This question already has answers here : What is the difference between O, Ω, and Θ? (6 answers) Closed 6 years ago . I'm new to computer science and just started with pseudocodes and I have some questions. It's my third week in the semester and majority is self-studying. I have some questions: What is the difference of an O(n^2) with an O(n) algorithm? - Similarly, what is O(n log n)? - and Ω(n^2)? So far, I have written: horner = 0; for( i = n; i >= 0; i −− ) horner = x * horner + a[i]; But