Context Free Grammar for language L
The solution in the question is correct.

The constraint $i\ne j$ is the one that gives us trouble. To get around it  we have to split into two cases: (i) $i&lt;j$, and (ii) $i&gt;j$ (but still $i&lt;2j$)

thus
$S\to S_{(i)} \mid S_{(ii)}$
as the first production splits into this two cases.

Now, divide and conquer:  case (i) is very simple, since we only need $i&lt;j$,
$S_{(i)} \to aS_{(i)}b \mid B$
$ B \to Bb \mid b$

This is your $S_2$.

For case (ii), we need to $j&lt; i &lt; 2j$, so for every single $a$, the variable $C$ will generate exactly one $b$, and at a certain point we switch to the variable $D$ that will generate two $a$'s for each $b$.
$S_{(ii)} \to aCb $
$ C \to aCb \mid D$
$ D \to aaDb \mid aab$

This is your $S_1$.