HackerRank Maximum Disjoint Subtree Product - algorithm

Can anybody give me a better explained insight about the problem Maximum Disjoint Subtree Product (link here) ?? I can't figure it out from the psetter analysis.
I don't understand 2nd DFS approach (it tries to calculate the solution for "up subtrees"??)
Please can someone help me and/or give me other problems following the same solution approach ??? I really appreciate other problems like this one. Thanks in advance :D

It seems like a dynamic programming problem. Check out this link before, it contains great tutorial on problems that involve dynamic programming on Trees.

Related

Precalculate Result of A*

Currently learning about the A* search algorithm and using it to find the quickest solution to the N-Puzzle. For some random seed of the initial starting state, the puzzle may be unsolvable which would result in extremely long wait times until the algorithm has search the entire search-space and determined there is not solution to the give start state.
I was wondering if there is a method of precalculating whether the A* algorithm will fail to avoid such a scenario. I've read a bit about how it is possible but can't find a direct answer as to a method in which to do it.
Any guidance or options are appreciated.
I think A* does not offer you a mechanism to know whether or not a problem is solvable. Specifically for N-Puzzle, I think this could help you to check if it can be solved or not:
http://www.geeksforgeeks.org/check-instance-8-puzzle-solvable/
It seems that if you are in a state where you have an odd amount inversion, you know for sure the problem for that permutation is infeasible.
For the N-puzzle specifically, there are only two possible parities, so you just need to check which parity the current puzzle is.
There is an in-depth explanation on how to do this on the math stackexchange
For general A* problems, no, there is no way to pre-compute if the graph is solvable.

Lagrange's four-square Implementation

I am learning about Lagrange theorem , According to which every number can be represent a sum of square , But how do i implement this algorithm , i have search the web but i could not find any relevant material on this. Can anyone help me implement this algorithm or provide some reference ?
It doesn't look like there's an easy answer for this. Here are some references I think will help, though.
https://math.stackexchange.com/questions/483101/rabin-and-shallit-algorithm
https://cs.stackexchange.com/questions/2988/how-fast-can-we-find-all-four-square-combinations-that-sum-to-n

Solving puzzle with tree data structure

I am currently following an algorithms class and we have to solve a sudoku.
I have already a working solution with naive backtracking but I'm much more interest in solving this puzzle problem with a tree data structure.
My problem is that I don't quite understand how it works. Is anyone can explain to me the basic of puzzle solving with tree?
I don't seek optimization. I looking for explanation on algorithms like the Genetic algorithm or something similar. My purpose only to learn at this point. I have hard time to take what I read in scientific articles and translate it on real implementation.
I Hope, I've made my question more clear.
Thank you very much!
EDIT: I edit the post to be more precise.
I don't know how to solve Sudoku "with a tree", but you're trying to mark as many cells as possible before trying to guess and using backtrace. Therefore check out this question.
Like in most search problems, also in Sudoku you may associate a tree with the problem. In this case nodes are partial assignments to the variables and branches correspond to extensions of the assignment in the parent node. However it is not practical (or even feasible) to store these trees in memory, you can think of them as a conceptual tool. Backtracking actually navigates such a tree using variable ordering to avoid exploring hopeless branches. A better (faster) solution can be obtained by constraint propagation techniques since you know that all rows, columns, and 3x3 cells must contain different values. A simple algorithm for that is AC3 and it is covered in most introductory AI books including Russell & Norvig 2010.

LCA problem at interview

Sometimes I come across interview questions like this: "Find the common parent of any 2 nodes in a tree". I noticed that they ask LCA questions also at Google, Amazon, etc.
As wikipedia says LCA can be found by intersection of the paths from the given nodes to the root and it takes O(H), where H is the height of the tree. Besides, there are more advanced algorithms that process trees in O(N) and answer LCA queries in O(1).
I wonder what exactly interviewers want to learn about candidates asking this LCA question. The first algorithm of paths intersection seems trivial. Do they expect the candidates to remember pre-processing algorithms ? Do they expect the candidates to invent these algorithms and data structures on the fly ?
They want to see what you're made of. They want to see how you think, how you tackle on problem, and handle stress from deadline. I suppose that if you solve the problem because you already know the solution then they will just pose you another problem. It's not the solution they want, it's your brainz.
With many algorithms questions in an interview, I don't care (or want) you to remember the answer. I want you to be able to derive the answer from the main principles you know.
Realistically: I could give two cares less if you already know the answer to "how to do X", if you are able to quickly construct an answer on the fly. Ultimately: in the real world, I can't necessarily assume you have experience tackling problems of domain X, but if you run across a problem in said domain, I will certainly hope you'll have the analytical ability to figure out a reasonable answer, based on the general knowledge you hold.
A tree is a common data structure it's safe to assume most will know - if you know the answer offhand, you should be able to explain it. If you don't know the answer, but understand the data structure, you should be able to fairy easily derive the answer.

Create a sum of 1000, 2000, etc. from set of numers

Ok, so here's the problem:
I need to find any number of intem groups from 50-100 item set that add up to 1000, 2000, ..., 10000.
Input: list of integers
Integer can be on one list only.
Any ideas on algorithm?
Googling for "Knapsack problem" should get you quite a few hits (though they're not likely to be very encouraging -- this is quite a well known NP-complete problem).
Edit: if you want to get technical, what you're describing seems to really be the subset sum problem -- which is a special case of the knapsack problem. Of course, that's assuming I'm understanding your description correctly, which I'll admit may be open to some question.
You might find Algorithm 3.94 in The Handbook of Applied Cryptography helpful.
I'm not 100% on what you are asking, but I've used backtracking searches for something like this before. This is a brute force algorithm that is the slowest possible solution, but it will work. The wiki article on Backtracking Search may help you. Basically, you can use a recursive algorithm to examine every possible combination.
This is the knapsack problem. Are there any constraints on the integers you can choose from? Are they divisible? Are they all less than some given value? There may be ways to solve the problem in polynomial time given such constraints - Google will provide you with answers.

Resources