It can be solved in O(nlogn). Just start from the end and find
decreasing subsequence of size k. For example Eg
arr[]={5,2,1,10,9,30,8,55}, start from the end and put the subsequence
elements inside a different array.
Following concept can be used effectively in the same manner but we
have to just
formatting changes .
void permute(char *a, int i, int n)
{
int j;
if (i == n)
printf("%s\n", a);
for (j = i; j <= n; j++)
{
if(a[i] != a[j]) check before swapping if you are
swapping different elements or not, if not then don't.
{ swap(a[i], a[j]);
void permute(char *a, int i, int n)
{
int j;
if (i == n)
printf("%s\n", a);
for (j = i; j <= n; j++)
{
if(a[i] != a[j])
{ swap(a[i], a[j]); //just check before swapping if
you are swapping different elements
//or