Ok, so I was looking at some exam problems that are given in order to get into a certain college. And I could solve them all but one , you can consider this a challenge if you like (please use spoiler tags) . Also, it requires only the algorithm so you should provide it in pseudocode.
Data: You are the following sequence of numbers 1,2,2,3,3,3,4,4,4,4,... Basically each number repeats itself accordingly to its value .
Requirement: Provide an algorithm that will return the Nth element of the sequence ( N will be given by input ) . The algorithm must run in a constant time ( it should not depend on the value of N )
I tried some wild ideas but it didn't work so far , so please have a try. Enjoy solving !
Exam Question
Page 1 of 13 Replies - 962 Views - Last Post: 26 September 2012 - 04:38 AM
Replies To: Exam Question
#2
Re: Exam Question
Posted 25 September 2012 - 12:26 PM
Interesting way to crowd source an exam answer. Have you thought about just two for loops and a counter?
#3
Re: Exam Question
Posted 25 September 2012 - 12:27 PM
put the thing in a loop and have the number become the increment variable. then also have the number in the loop to print the variable as many times as the loop runs.
#4
Re: Exam Question
Posted 26 September 2012 - 04:38 AM
Actually the solution looks like this .
Since the numbers go like 1, 2, 2, 3, 3, 3, 4, 4, 4, 4,etc.
We can write n like this:
x(x+1) / 2 = n then we find out how much x is and so we'll have an = x if x is integer and an = [x] + 1 if x is float .
Since the numbers go like 1, 2, 2, 3, 3, 3, 4, 4, 4, 4,etc.
We can write n like this:
x(x+1) / 2 = n then we find out how much x is and so we'll have an = x if x is integer and an = [x] + 1 if x is float .
Page 1 of 1