Skip to main content
3 of 3
added 145 characters in body
Anjan Kumar
  • 5k
  • 1
  • 17
  • 29

This answer is faster than @Roman's by at least a magnitude.

fa[l_] := Module[{twiceSelect, pos, lDuplicate = l, outputList = {}},

     (*Creates an association with positions as values*)
     (*pos = First /@ PositionIndex[l];*) (* If there are same elements in the list this would fail*)
      pos = Thread[l->Range[Length[l]]]//Association; (*This is more robust*)
      
      (*A helper function to select the first element 
        which satisfies the condition that it should be 
        twice than the first element of the list.*)
     twiceSelect[x_] := SelectFirst[Rest[x], # > 2*First[x] &] /. _Missing -> {};
      
      (*Here for every loop, you apply twiceSelect[], 
        truncate the list, and so on, for Length[l] times*)
      Do[
          AppendTo[outputList, twiceSelect[lDuplicate]];
          lDuplicate = Rest[lDuplicate];,
          Length[l]
        ];

       (*After obtaining all the required elements, replace the
        elements with their corresponsing positions*)
       outputList /. pos
      ]

Test

SeedRandom[1234]
list = RandomReal[{-100, 100}, 10];
fa[list]

{{}, 6, 4, 6, 6, {}, 10, 10, 10, {}}

Timing Comparison

SeedRandom[1234]
list = RandomReal[{-100, 100}, 1000];
    
fr[LL_]:=Lookup[GroupBy[
   SequencePosition[LL, {e_, ___, f_} /; f >= 2 e, Overlaps -> All], 
  First -> Last, Min], Range[Length[LL]], {}
]; (*Roman's*)

m = fr[list];//AbsoluteTiming
n = fa[list];//AbsoluteTiming
m == n

{4.32954, Null}

{0.122447, Null}

True

Anjan Kumar
  • 5k
  • 1
  • 17
  • 29