Necessary and sufficient conditions for the ghs inequality with applications to analysis and probability