generate functionTue, 18 Apr 2023

Some people are standing in a row in a park. There are trees between them which cannot be moved. Your task is to rearrange the people by their heights in a non-descending order without moving the trees. People can be very tall!

int[] sortByHeight(int[] a) { List<int> trees = new List<int>(); List<int> people = new List<int>(); for (int i = 0; i < a.Length; i++) { if(a[i] == -1) trees.Add(i); else people.Add(a[i]); } people.Sort(); int index = 0; foreach(int i in trees) { a[i] = -1; } foreach(int i in people) { a[index] = i; index++; } return a; }

Questions about programming?Chat with your personal AI assistant