Member-only story

Divide method: Rearrange the elements of the array so that all the elements whose value is < divider come before all the other elements, and all the elements whose value is > divider come after all the other elements

Marika Lam
2 min readMay 4, 2022

int divide(string a[], int n, string divider);

Rearrange the elements of the array so that all the elements whose value is < divider come before all the other elements, and all the elements whose value is > divider come after all the other elements. Return the position of the first element that, after the rearrangement, is not < divider, or n if there are no such elements. Here's an example:

string sc[6] = { "juliet", "alpha", "samuel", "elena", "sonia", "november" };int x = divide(sc, 6, "kilo");  //  returns 3// sc must now be "elena"  "juliet"  "alpha"  "november"  "sonia"  "samuel" or "juliet"  "alpha"  "elena"  "samuel"  "november"  "sonia" or one of several other orderings.// All elements < "kilo" (i.e., "juliet", "alpha", and "elena") come before all others// All elements > "kilo" (i.e., "sonia", "november", and "samuel") come after all othersstring sc2[4] = { "juliet", "sonia", "alpha", "november" };int y = divide(sc2, 4, "november");  //  returns 2// sc2 must now be either "juliet"  "alpha"  "november"  "sonia" or "alpha"  "juliet"…

--

--

No responses yet