An array of N elements is given indexed from 1 to N. All elements are unknown and integers. Given some queries in the form of A, B, C where A is the starting index, B is the ending index and C is the sum of all elements between A and B inclusive. Find out all the elements of array. Example:
N=4 1, 3, 0 2, 4, 4One valid solution to this is:
2, -3, 1, 6Constraints:
1<=A<=B<=N, 2<=N<=65000, C<=1000000000Any solution fulfilling the given criteria is accepted and assume enough queries are given to find out all the elements.
An array of N elements is given indexed from 1 to N. All elements
Share
One way to solve this is by treating the problem as a series of simultaneous equations. Each summation gives you a linear equation in up to n variables, so if you can find a solution to that equation that has integer values you should be all set.
Using Gaussian Elimination on a series of n variables with k different constraints takes (assuming that n = O(k)) O(k3) time on expectation (assuming the system is well-conditioned). From there, finding integer solutions should be easy; just find the common denominator of any one solution vector and multiply through by it.
Hope this helps!