I want to write a method that returns the last odd position available that has not been removed. For example
Say the number inputted to function is 5
Then we can number as follows 1,2,3,4,5
After first pass, numbers left are - 2,4. 1,3,5 have been removed
After second pass - 2 has been removed and so we are left with 4
The function should return 4
. In my method, I create an array of n numbers. Each element may either have a value 0 or 1, 0 representing that element not being removed and 1 being removed.My code is
int findposition(int n)
{
int barr[n];
int count=0,i,begIndex=0;
for(i=0;i<n;i++)
barr[i]=0;
while(n!=count+1)
{
for(i=begIndex;i<n;i=i+2)
{
barr[i]=1;
}
for(i=0;i<n;i++)
{
if(barr[i]==0)
begIndex=i;
}
}
return (begIndex+1);
}
The above function compiles successfully, but does not print anything when called from main method. Also, I think my approach is a bit clumsy. Can there be a more neater way to do this.