we have to input n numbers as per users choice like if n=4 we input the numbers 8,7,5,2 make the first number zero and move that many steps ahead like 8 is when made zero and we move 8 steps we move to 5(dont include 8 again)and we make 5 zero and then move 5 steps and come to 2 and then the number which has not been zero till last is the lucky number (here the answer is 7)
please provide a link to the question. It depends upon constraints. If they are small a simple brute force will work. But If they are large, then will have to think on a mathematical approach.
the question is given by school teacher and there is no link to the question
So u’re getting your homework done by us? XD hehee
Please explain your problem so that people can understand atleast what you are trying to say…I really dont get your question
the question is that we have to input n numbers (zero cant be input)in array .for example we input 4 numbers (n=4)the numbers are 8,7,5,2.we have to move 8 steps ahead whichever number is first move that many steps and make that number zero when we move 8 steps we come to 5 and make 5 equal to 0 and then move 5 steps in array then we come to 2 and then make 2 equal to zero and move 2 steps (we cant move 2 steps as all numbers are zero except one number that is 7 )so 7 is the lucky number.
please help
It’s a standard josephus problem which can easily be done with help of circular linked list. just create a link list with the numbers given and then join the end with start and remove the first and iterate to the list and get the next position until the list have only one element.
see this for better understanding. Hope this helps!
dear, don’t use award points. you can accept the answer if it helps.
the answer you told me is ok but here we have to input different numbers and here is no pattern in numbers as in case of josephus promblem
yes, but you can always implement naive algorithm. see just create the link list like josephus using the user input and then just start from beginning and then remove it and loop that many time (the number you removed) i.e. just do “while(number --) head = head -> next” and then repeat the same for the current head.
thanks for help