Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
Given a list of 2n-1 numbers: all between 1 to n, all but one occur twice. Determine the number that occurs only once. Multiple ways preferred.
I think the problem is at fault, how can you determine which number without knowing the list of numbers?
[O(1) space, O(n) time]: Just take the XOR of all the numbers. Since all the numbers occur two times except one, XOR of those numbers will be zero and the single occurring number will be the result.
[O(1) space, O(n) time]: As said by user3386109 in comments, we can sum all the given numbers and compare that to the sum of numbers in the range [1, n] which will be n*(n+1) (since all numbers are supposed to occur twice). The difference of the two numbers is the answer.
[O(n) space, O(n) time]: Create an array of size n and keep the count of all the elements in the array at their corresponding positions. At the end, traverse the array, and find the number whose count is only 1.
Related
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 6 years ago.
Improve this question
How many unique arrays of m elements exist such that they contain numbers in the range [1,n] and there exists atleast one subsequence {1,2,3,4....n}?
Constraints: m > n
I thought of combinations approach. But there will be repetitions.
In my approach, I first lay out all the numbers from 1 to n.
For example, if m=n+1, answer is n^2. (n spots available, each number in range [1,n])
Now, I think there might be a DP relation for further calculation, but I am not being able to figure it out.
Here's an example for n=3 and m=5. The green squares are the subsequence. The subsequence consists of the first 1 in the array, the first 2 that's after the first 1, etc. Squares that aren't part of the subsequence can either take n values if they are after the end of the subsequence, or n-1 values otherwise.
So the answer to this example is 1*9 + 3*6 + 6*4 = 51, which is easily verified by brute force. The coefficients 1,3,6 appear to be related to Pascal's triangle. The rest is left to the reader.
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 years ago.
Improve this question
Call every subunitary ratio with its denominator a power of 2 a perplex.
Number 1 can be written in many ways as a sum of perplexes.
Call every sum of perplexes a zeta.
Two zetas are distinct if and only if one of the zeta has as least one perplex that the other does not have. In the image shown above, the last two zetas are considered to be the same.
Find all the numbers of ways 1 can be written as a zeta with N perplexes. Because this number can be big, calculate it modulo 100003.
Please don't post the code, but rather the algorithm. Be as precise as you can.
This problem was given at a contest and the official solution, written in the Romanian language, has been uploaded at https://www.dropbox.com/s/ulvp9of5b3bfgm0/1112_descr_P2_fractii2.docx?dl=0 , as a docx file. (you can use google translate)
I do not understand what the author of the solution meant to say there.
Well, this reminds me of BFS algorithms(Breadth first search), where you radiate out from a single point to find multiple solutions w/ different permutations.
Here you can use recursion, and set the base case as when N perplexes have been reached in that 1 call stack of the recursive function.
So you can say:
function(int N <-- perplexes, ArrayList<Double> currentNumbers, double dividedNum)
if N == 0, then you're done - enter the currentNumbers array into a hashtable
clone the currentNumbers ArrayList as cloneNumbers
remove dividedNum from cloneNumbers and add 2 dividedNum/2
iterate through index of cloneNumbers
for every number x in cloneNumbers, call function(N--, cloneNumbers, x)
This is a rough, very inefficient but short way to do it. There's obviously a lot of ways you can prune the algorithm(reduce the amount of duplicates going into the hashtable, prevent cloning as much as possible, etc), but because this shows the absolute permutation of every number, and then enters that sequence into a hashtable, the hashtable will use its equals() comparison to see that the sequence already exists(such as your last 2 zetas), and reject the duplicate. That way, you'll be left with the answer you want.
The efficiency of the current algorithm: O(|E|^(N)), where |E| is the absolute number of numbers you can have inside of the array at the end of all insertions, and N is the number of insertions(or as you said, # of perplexes). Obviously this isn't the most optimal speed, but it does definitely work.
Hope this helps!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
How to check if a number is a power of 5?
I could think of below algorithm. Is there way to improve it? Any mathematical trick?
First check if last digit is 5.
If last digit is 5; divide it by 5.
If result of division is 1, then number is power of 5.
Else check if division result itself is power of 5 (i.e. go to step 1 with result as number).
You don't need to look at individual digits, you can just do it like this:
n = (int)(log(x) / log(5)); // get n = log5(x), truncated to integer
if (pow(5, n) == x) // test to see whether x == 5^n
// x is a power of 5
LIVE DEMO
There are only few power of five that fit in int/long range, so you just need to generate all of them and check one by one (less than 60 numbers), using a HashSet will have O(1) time complexity
Successive division until you reach the number undivided by 5 and check whether the result is equal to 1, isn't bad solution. It take log_5(n) operations, so it's O(lg n), it's very good time. For 9094947017729282379150390625 it's only 40 operations!
What I would do is first create an array of numbers that are powers of 5. You could use a range, and then say, for each value in the range, take that value to the fifth power, and push the new value into the array.
You would then find if n, the number you are looking for is included in the array.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I was asked to give an algorithm that was supposed to be O(n(log(k)))
k is the number of arrays and n is the total number of elements in all of these. I had to sort the arrays.
Minus the details I came up with an algorithm that does the job for in klog(k) times the total number of elements. i.e. O(nk(log(k)))
Also in this case k is much smaller than n so it wont be n^2(logn) (in case k and n were almost same)right?
Well, no, it's not the same. If k is a variable (as opposed to a constant) in the complexity expression then O(nk(log(k))) > O(n(log(k))).
That is because there is no constant C such that Cn(log(k)) > kn(log(k)) for every n, k.
The way you describe the question both k and n are input parameters. If that is the case then the answer to your question is
'No, O(n*k *log(k)) is not the same as O(n*log(k))'.
It is not that hard to see that the first one grows faster than the second one, but it is even more obvious if you fix the value of n. Consider n begin a constant say 1. Than it is more obvious that O(k*log(k)) is not the same as O(log(k)).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Write a program that will read in five positive integers (one at a time) and print out the largest and smallest number of the five numbers. The program should read the numbers one at a time.
Mind you this is pseudocode and not to be done in any language.
My question is how would I go about setting this up so that the 5 integers save as values so I can display them.
Don't want the answer, just a start.
How would you solve the same problem if you had to only report the largest number? The pseudocode would be something like the following
consider the first number to be largest
for each of the rest of the number
if it is larger then the current largest
assign to largest
How would you do it if there were two?
consider the first number to be largest
if second number is larger then the largest
consider the second number to be largest, first to be 2nd largest
else
consider the first number to be largest, second to be 2nd largest
for each of the rest of the numbers
if it is larger then the largest
consider current largest to be 2nd largest and this number to be largest
else if it is larger then the 2nd largest
consider it to be 2nd largest
But if there are three or more this can get ugly. How do we keep N largest number? Clearly, we need a list of N sorted number. I will leave it to you how to maintain that list, but here's a pseudocode using that approach
populate the top-list with first N numbers from input, ensure the top-list is sorted
for each of the rest of the numbers
if the number is larger then any number in the top-list
insert it at the right place in top list, pushing out the smallest element of the top list
The question now is: is this better than sorting the list and picking up the top N and bottom N elements?
The answer is that "it depends". Can you figure out some circumstances where one approach is better then the other?
As you read the numbers, keep track of the currently largest and smallest numbers, and update the values as the input is coming in. This has the advantage that it works for even long sequences of numbers. I mean something like this:
min = 0
max = 0
while input:
read number from input
if number < min:
min = number
if number > max:
max = number