algorithm attempt with only case 1