locked
4th quiz - Questions and doubts RRS feed

  • Question

  • My 2nd Question was

     

    Lucky Numbers are positive integers obtained by the following process: Take the set of positive integers and remove every 2nd number. From what is left, the 2nd number is 3, so delete every 3rd number and so on until no more numbers can be removed. The remaining numbers are known as Lucky Numbers.

    Write a program to generate the lucky numbers from 1 to n.

    Constraints:
    n <= 1 and n >= 10000

    Example 1
    Input:
    int n = 25
    Output
         Returns {1, 3, 7, 9, 13, 15, 21, 25}
    Explanation: Delete every 2nd number, leaving {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25}. The 2nd number in the series is 3, so delete every 3rd numbers, leaving {1, 3, 7, 9, 13, 15, 19, 21, 25}. The 3rd number in the series is 7, hence delete every 7th numbers, leaving {1, 3, 7, 9, 13, 15, 21, 25}. The 4th number is 9 and in the series there is only 8 numbers and the final output is {1, 3, 7, 9, 13, 15, 21, 25}

    Example 2
    Input:
    int n = 60
    Output
         Returns {1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51}

    For C solutions 
    Header File : LuckyNumber.h
    Function Name : int* getLuckyNumber(int n) 
    Directory Name : luckynumber
    File Name : LuckyNumber.c
    For C++ solutions 
    Header File : LuckyNumber.h
    Class Name : lucky
    Function Name : int* getLuckyNumber(int n)
    Directory Name : luckynumber
    FileName : LuckyNumber.cpp



     

    My doubt is how feasible is it to return an integer pointer.. (Suppose that we return a character pointer for some problem, we can get to know of the end of the character array with the help of NULL CHARACTER..).. But here we should assume some number such as 0 to indicate the end of the expected o/p...

    Even in that case the returned  o/p from the function will be {1, 3, 7, 9, 13, 15, 21, 25,0,x,x x,x .......} ...

     

    Am confused a lot... Please help.. Correct me if am wrong....

    Monday, May 21, 2007 12:12 AM

Answers

  •  

    U could have used a simple array which is in the function of integer pointer....lolz

     

     

    Monday, May 21, 2007 1:12 AM
  • @ Karan - even if i use a simple array and return it , how does one find the end of the valid output in the returned array.. One would need to use explicit end of array such as a 0 to indicate the end of valid output...

     

    Correct me if am wrong...

    Tuesday, May 22, 2007 5:33 AM
  • This is what i did in a similar type of the problem.. I took a globle variable size to keep the size of the array.. inside the function i updated the size of the array at the end, this way i kept a track of the size of the array.. but still, while giving the question, they should have taken care of it.. like including a variable pointer for the size, as using a globle variable is not a good option... and nor is it advisable...
    Tuesday, May 22, 2007 6:58 PM
  • @ Varun - Ya, I agree with u Varun. But how will they evaluate our program?

     

    It seems they will consider  only the function for evaluation.. They will give a input to the function and check for the output of the function... In that case, the global variable will not be considered...

     

    Correct me if am wrong

    Wednesday, May 23, 2007 2:15 AM
  • Even i thought so.. but when they have not given us any option, then it is not our headache to think over it... so i had left it upto them.. i had even asked the live help person if i could just display the answer from the function, just to show the answer.. he asked me to do it from main only.. no display from the called function.. so what else could we do??
    Wednesday, May 23, 2007 3:34 AM
  • I think i can throw some light on it. The admins dont care what we did in the void main. they just care about whether the function works appropriately or not. I think they have a script that automatically call our functions and then evaluate the results.


    Wednesday, May 23, 2007 5:12 PM
  • With automated scripts , it is still grave.. Our outputs will be considered wrong since it expects say {1,2,3} as output but our function will return {1,2,3,x,y..}..
    Wednesday, May 23, 2007 5:24 PM
  • I took care of it, by 1st creating a temporary array, then at the end creating a dynamic array of the required size, copying the answer and then returning the new array...
    Wednesday, May 23, 2007 5:43 PM
  • Thats really a nice way of doing it Varun Smile

    But i think the admins will have another function that will count the number of elements that will be returned , so that they can check it from there Smile
    Wednesday, May 23, 2007 6:09 PM
  • Hmmm I dont think so.. tell me how would you do it?? unless you specify the length of the array, there is no way to find out the length by just the address of the base element of the array..
    Ya but if you know what can be the probable size of the array before hand and try to search only those many elements than its ok.. but its highly inefficient way to do so...
    Thursday, May 24, 2007 9:45 AM
  • @Varun - Remember in last or last to last quiz we had to create two functions ? one for the main logic, and the other for counting the number of elelments the answer would have ?

    I know its an inefficient way to do it, but there is no other way the admins can test it using an automized procedure. I hope you get my point. The point i wanted to put forward is that the admin might have the number of elements that the answer should return, may be they have implemented a function for that, and then they can try to match our answer with it.

    Also to verify our answers, they would have implemented a similar feature in their code as well, so as the loop goes, they can keep verifying our elements of the array that we returned.
    Thursday, May 24, 2007 10:25 AM
  • But Harshil There is always a better way.. pass the reference pointer to a variable size in the function.. then at the end, you update this variable, which will show the size of the array.. this way you will get all the work done. you can also implement this in the main function.. this way you wont have to worry about how to implement it, as it will be a standard way for everyone...
    Thursday, May 24, 2007 11:21 AM
  • @Varun - Good idea. It will really work, but for our purpose only . The prototype of the function that we were given in the quiz is fixed, and we cannot do any modifications to that. The question here is not how to find out the number of elements returned as the result, the question here is how the admin can know how many elements have been returned from OUR function , Cause they use automated scripts for checking the validity of your functions. They should have means to know about the number of elements.

    Someone asked how the admins will check the solutions earlier, so i wanted to point out by giving an example Smile

    What i am trying to say, you already know, and what you are trying to say, i also know. I think its waste of both our time in discussing what we already know. I think we should discuss something else Wink
    Thursday, May 24, 2007 5:59 PM
  • hahha...i liked the last two sentences of yours harshil, i mean the way they were connected and rhyming....it reminded me of ITCS [introduction to theoretical computer science]. In that we used to have all those tricky questions....and all those going round and round questions...
    Friday, May 25, 2007 12:44 PM

All replies

  •  

    U could have used a simple array which is in the function of integer pointer....lolz

     

     

    Monday, May 21, 2007 1:12 AM
  • @ Karan - even if i use a simple array and return it , how does one find the end of the valid output in the returned array.. One would need to use explicit end of array such as a 0 to indicate the end of valid output...

     

    Correct me if am wrong...

    Tuesday, May 22, 2007 5:33 AM
  • This is what i did in a similar type of the problem.. I took a globle variable size to keep the size of the array.. inside the function i updated the size of the array at the end, this way i kept a track of the size of the array.. but still, while giving the question, they should have taken care of it.. like including a variable pointer for the size, as using a globle variable is not a good option... and nor is it advisable...
    Tuesday, May 22, 2007 6:58 PM
  • @ Varun - Ya, I agree with u Varun. But how will they evaluate our program?

     

    It seems they will consider  only the function for evaluation.. They will give a input to the function and check for the output of the function... In that case, the global variable will not be considered...

     

    Correct me if am wrong

    Wednesday, May 23, 2007 2:15 AM
  • Even i thought so.. but when they have not given us any option, then it is not our headache to think over it... so i had left it upto them.. i had even asked the live help person if i could just display the answer from the function, just to show the answer.. he asked me to do it from main only.. no display from the called function.. so what else could we do??
    Wednesday, May 23, 2007 3:34 AM
  • I think i can throw some light on it. The admins dont care what we did in the void main. they just care about whether the function works appropriately or not. I think they have a script that automatically call our functions and then evaluate the results.


    Wednesday, May 23, 2007 5:12 PM
  • With automated scripts , it is still grave.. Our outputs will be considered wrong since it expects say {1,2,3} as output but our function will return {1,2,3,x,y..}..
    Wednesday, May 23, 2007 5:24 PM
  • I took care of it, by 1st creating a temporary array, then at the end creating a dynamic array of the required size, copying the answer and then returning the new array...
    Wednesday, May 23, 2007 5:43 PM
  • Thats really a nice way of doing it Varun Smile

    But i think the admins will have another function that will count the number of elements that will be returned , so that they can check it from there Smile
    Wednesday, May 23, 2007 6:09 PM
  • Hmmm I dont think so.. tell me how would you do it?? unless you specify the length of the array, there is no way to find out the length by just the address of the base element of the array..
    Ya but if you know what can be the probable size of the array before hand and try to search only those many elements than its ok.. but its highly inefficient way to do so...
    Thursday, May 24, 2007 9:45 AM
  • @Varun - Remember in last or last to last quiz we had to create two functions ? one for the main logic, and the other for counting the number of elelments the answer would have ?

    I know its an inefficient way to do it, but there is no other way the admins can test it using an automized procedure. I hope you get my point. The point i wanted to put forward is that the admin might have the number of elements that the answer should return, may be they have implemented a function for that, and then they can try to match our answer with it.

    Also to verify our answers, they would have implemented a similar feature in their code as well, so as the loop goes, they can keep verifying our elements of the array that we returned.
    Thursday, May 24, 2007 10:25 AM
  • But Harshil There is always a better way.. pass the reference pointer to a variable size in the function.. then at the end, you update this variable, which will show the size of the array.. this way you will get all the work done. you can also implement this in the main function.. this way you wont have to worry about how to implement it, as it will be a standard way for everyone...
    Thursday, May 24, 2007 11:21 AM
  • @Varun - Good idea. It will really work, but for our purpose only . The prototype of the function that we were given in the quiz is fixed, and we cannot do any modifications to that. The question here is not how to find out the number of elements returned as the result, the question here is how the admin can know how many elements have been returned from OUR function , Cause they use automated scripts for checking the validity of your functions. They should have means to know about the number of elements.

    Someone asked how the admins will check the solutions earlier, so i wanted to point out by giving an example Smile

    What i am trying to say, you already know, and what you are trying to say, i also know. I think its waste of both our time in discussing what we already know. I think we should discuss something else Wink
    Thursday, May 24, 2007 5:59 PM
  • hahha...i liked the last two sentences of yours harshil, i mean the way they were connected and rhyming....it reminded me of ITCS [introduction to theoretical computer science]. In that we used to have all those tricky questions....and all those going round and round questions...
    Friday, May 25, 2007 12:44 PM
  • Looks like Harshil likes to hear lots of songs
    Friday, May 25, 2007 1:43 PM
  • thanks Anoop, mention not Wink
    Friday, May 25, 2007 2:03 PM