Also, preserve the original relative order of elements in both the groups (i.e elements smaller than 'x' form one group and elements equal to or greater than 'x' form another group. The relative order should be maintained in both the groups.)
Example 1:-
a={2,6,3,5,1,7}
x=5
Output : 2 3 1 6 5 7
All the elements smaller than 5 come before 5 and the relative order of the moved elements is
preserved (2,3,1) & (6,5,7)
Example 2:-
a={1,4,2,5,3}
x=4
output : 1 2 3 4 5
The original question was for a singly linked list . I wrote the algorithm for an array and I wanted to know if it can be ported to the linked list variation. Also, is there a better way of doing this?
#include <bits/stdc++.h>
using namespace std;
void swap(vector<int> &a, int i, int j)
{
int i2 = i;
int x = a[j];
int temp1 = a[i];
int temp2;
while (i < j)
{
temp2 = a[i + 1];
a[i + 1] = temp1;
temp1 = temp2;
i++;
}
a[i2] = x;
}
void solve(vector<int> &a, int num)
{
int n = a.size();
int i = 0, j = 1;
while (j < n)
{
if (a[i] < num)
i++;
if (a[i] >= num && a[j] < num)
swap(a, i, j);
j++;
}
}
int main()
{
vector<int> a = {2, 6, 3, 5, 1, 7};
int num = 5;
solve(a, num);
for (auto el : a)
cout << el << " ";
return 0;
}