Grid Search java solution