Google
Information Storage and Retrieval: Search in the Sequence

Pages

Monday, October 8, 2007

Search in the Sequence

There is an infinite sequence of numbers as follows :

1,2,2,3,3,3,4,4,4,4,5,5,5,5,5......(the 'i' number appears i times) . If you are given a number 'k', write a program that will return the kth number in the sequence given above.

e.g if k=4 then answer will be 3.

4 comments:

Maverick Psyche said...

if kth element is to be found then

for (i=1, i<=k i++)
{
for (j=1 j<=i j++)
{
VALUE = i
k=k-1
IF ( k=0)
break;
}
}

PRINT VALUE;

there mightr be other soln. with in While loop or something like that.

Maverick Psyche said...

ONE MORE SOLN.:-

AS N*(N+1)/2 = sum up tp N digits

if kth element is to be found then

N*(N+1)/2 = K

N2(Npower2)+ N - 2K >=0 then N is the print value.

so

for (i=1, i<=k i++)
{
TEMP = N2(Npower2)+ N - 2K

IF (TEMP >= 0)
break;

}
PRINT i

Unknown said...

AAh Ah!!
Tech/Pogramming blog..gr8 going mate..

count=0;
for(i=1,i<=k&&count<=k,i++)
{
temp=0;
for(j=i,j>0,j--)
{
temp++;
}
count=count+temp;
}
print temp; //shud give u the value at k'th position

not sure if it works..never tried it on comp..jus on paper.lemme know if der is something wrong in it.

Gaurav Kant Goel said...

@Jai: Its fine Jai... :)