Suppose the length of array
is dynamic and the elements follow the same pattern where by the next element is half the previous element. For example 1024, 512, 256, 128...
I would like to directly determine the index of an element. For example if I have 512
I would output index 1
without looping through the elements and comparing them with 512 then output 1. i.e not like this:
for (int i = 0; i < length; ++i) {
if (array[i] == 512) {
printf("%d\n", i);
break;
}
}
I have been thinking of using modulus or bit manipulation like shifts but I can't get it to work. How can this be achieved?