the following pseudocode may work:
for(i=l;i>=0;i--)
{
  if(a[i-1]<a[i])
     {
        swap(a[i-1],a[i]);
        sort(a,i,l);
        swap(a[i-1],a[l]);
        break;
      }
}

here sort is a function that sort the array from index i to l.....

On Thu, Sep 22, 2011 at 4:38 PM, bharatkumar bagana
<bagana.bharatku...@gmail.com> wrote:
> @deeraj : for i/p 7585 u'r code is giving 7855 ..
>
> On Thu, Sep 22, 2011 at 6:54 AM, algo geek <geeka...@gmail.com> wrote:
>>
>> Keep a pointer ont the rightmost digit. Keep moving right until you find
>> the number in increasing number. As soon as this breaks. stop. Swap the
>> digit at the current position with the smallest digit, but larger than the
>> current position digit, sort the array to the right of the current position
>> in descending order.
>>
>> explanation:
>> NUMBER:759854
>> INDEX:     012356
>>
>> Keep the pointer at index 6. Keep moving right as long as numbers are in
>> increasing fashion. Stop at index 1.
>> swap this digit with the smallest digit larger than 5 i.e. 8
>> 78(9554)
>> now sort the array in parenthesis in descending order
>> ans : 784559
>>
>> For the implementation you can actually create an array of length 10, to
>> keep track of all the digits encounters while moving left. This will help in
>> sorting as well (similar to counting sort).
>> One pass will do the work. You can print the answer directly afterwards.
>>
>> With Regards
>> Unknown
>>
>> On Thu, Sep 22, 2011 at 4:13 PM, Dheeraj Sharma
>> <dheerajsharma1...@gmail.com> wrote:
>>>
>>> in nlog(n)
>>> #include<iostream>
>>> #include<conio.h>
>>> #include<string.h>
>>> #define swap(a,b,c) (c)=(a),(a)=(b),(b)=(c)
>>> using namespace std;
>>> int t;
>>> void quicksort(char arr[],int left,int right)
>>> {
>>>      if(left<right)
>>>      {
>>>                    int piv=right;
>>>                    int i=left,j=left;
>>>                    while((i<right) && (j<right))
>>>                    {
>>>                     if(arr[j]<arr[piv])
>>>                     {
>>>                     swap(arr[i],arr[j],t);
>>>                     i++;
>>>                     }
>>>                     j++;
>>>                    }
>>>                    swap(arr[i],arr[piv],t);
>>>                    quicksort(arr,left,i-1);
>>>                    quicksort(arr,i+1,right);
>>>                    }
>>>      }
>>> int main()
>>> {
>>>     char str[100],t;
>>>     int arr[100],i=0;
>>>     cin>>str;
>>>     int min=0;
>>>     int len=strlen(str);
>>>     while(str[i])
>>>     {
>>>                  if(str[i]<=str[min])
>>>                  min=i;
>>>                  arr[i]=min;
>>>                  i++;
>>>                  }
>>>      i=len-1;
>>>      while((arr[i]==i)&&(i>0))
>>>      i--;
>>>      swap(str[i],str[arr[i]],t);
>>>
>>> if(i>0)
>>> quicksort(str,arr[i]+1,len-1);
>>>     cout<<str;
>>>  getch();
>>> }
>>>
>>>
>>> On Thu, Sep 22, 2011 at 3:33 PM, Dheeraj Sharma
>>> <dheerajsharma1...@gmail.com> wrote:
>>>>
>>>> #include<iostream>
>>>> #include<conio.h>
>>>> #include<string.h>
>>>> #define swap(a,b,c) (c)=(a),(a)=(b),(b)=(c)
>>>> using namespace std;
>>>> int main()
>>>> {
>>>>     char str[100],t;
>>>>     cin>>str;
>>>>
>>>>     int len=strlen(str);
>>>>  int i,x,boo=1;
>>>>     for(i=len-1;i>0&&boo;i--)
>>>>     {
>>>>         for(x=i-1;x>=0&&boo;x--)
>>>>         {
>>>>             if(str[x]<str[i])
>>>>             {
>>>>             swap(str[x],str[i],t);
>>>>             boo=0;
>>>>             }
>>>>             }
>>>>             }
>>>> if(i>0)
>>>> {
>>>>
>>>> //Sorting...
>>>>            for(int l=x+2;l<len-1;l++)
>>>>            {
>>>>                    int min=l;
>>>>                    for(int k=l+1;k<len;k++)
>>>>                    {
>>>>                            if(str[k]<str[min])
>>>>                            min=k;
>>>>                            }
>>>>                    swap(str[min],str[l],t);
>>>>                    }
>>>> }
>>>>     cout<<str;
>>>>
>>>>
>>>>  getch();
>>>> }
>>>>
>>>>
>>>> correct me..if m wrong..
>>>>
>>>> On Thu, Sep 22, 2011 at 2:01 PM, Ratan <success.rata...@gmail.com>
>>>> wrote:
>>>>>
>>>>> @dheeraj ... ya u r ryt.... thnxs for the correction
>>>>>
>>>>> On Thu, Sep 22, 2011 at 1:30 PM, Dheeraj Sharma
>>>>> <dheerajsharma1...@gmail.com> wrote:
>>>>> > @Ratan:
>>>>> > i think the next largest here would be 784559
>>>>> >
>>>>> > On Thu, Sep 22, 2011 at 12:39 PM, Ratan <success.rata...@gmail.com>
>>>>> > wrote:
>>>>> >>
>>>>> >> @kartik : to some extent ur code is giving the right answer... btw
>>>>> >> somehow check tis
>>>>> >> let for example the no be 759854
>>>>> >> then the next biggest no is 794558
>>>>> >> btw ur program is giving 795854 which is undoubtedly
>>>>> >> wrong............
>>>>> >> the code would give more appropriate result if u sort the numbers
>>>>> >> from
>>>>> >> from i to n on meeting the condition of (a[i-1]<a[i])
>>>>> >>
>>>>> >> On Thu, Sep 22, 2011 at 11:53 AM, Ramakant Sharma
>>>>> >> <ramakant...@gmail.com>
>>>>> >> wrote:
>>>>> >> > starting from right find first digit less then right most digit,if
>>>>> >> > no
>>>>> >> > any
>>>>> >> > digit is less,then move to next right most and compair ,,,,when
>>>>> >> > found
>>>>> >> > exchange those no,
>>>>> >> > now sort the no.s up to that index of the given no which is
>>>>> >> > exchanged:
>>>>> >> > Ex:
>>>>> >> > 43987723893239876
>>>>> >> > first required sequence: 439877238932[3]987[6] swap these no
>>>>> >> > 439877238932[6]{987[3]}
>>>>> >> > now sort in decreasing order  439877238932[6]{3789} this is the
>>>>> >> > required
>>>>> >> > no
>>>>> >> > ....correct me if any thing wrong
>>>>> >> >
>>>>> >> >
>>>>> >> >
>>>>> >> >
>>>>> >> >
>>>>> >> > --
>>>>> >> > You received this message because you are subscribed to the Google
>>>>> >> > Groups
>>>>> >> > "Algorithm Geeks" group.
>>>>> >> > To post to this group, send email to algogeeks@googlegroups.com.
>>>>> >> > To unsubscribe from this group, send email to
>>>>> >> > algogeeks+unsubscr...@googlegroups.com.
>>>>> >> > For more options, visit this group at
>>>>> >> > http://groups.google.com/group/algogeeks?hl=en.
>>>>> >> >
>>>>> >>
>>>>> >>
>>>>> >>
>>>>> >> --
>>>>> >> Ratan Kumar
>>>>> >> B. Tech
>>>>> >> MNNIT,
>>>>> >> Allahabad
>>>>> >>
>>>>> >> --
>>>>> >> You received this message because you are subscribed to the Google
>>>>> >> Groups
>>>>> >> "Algorithm Geeks" group.
>>>>> >> To post to this group, send email to algogeeks@googlegroups.com.
>>>>> >> To unsubscribe from this group, send email to
>>>>> >> algogeeks+unsubscr...@googlegroups.com.
>>>>> >> For more options, visit this group at
>>>>> >> http://groups.google.com/group/algogeeks?hl=en.
>>>>> >>
>>>>> >
>>>>> >
>>>>> >
>>>>> > --
>>>>> > Dheeraj Sharma
>>>>> > Comp Engg.
>>>>> > NIT Kurukshetra
>>>>> >
>>>>> >
>>>>> > --
>>>>> > You received this message because you are subscribed to the Google
>>>>> > Groups
>>>>> > "Algorithm Geeks" group.
>>>>> > To post to this group, send email to algogeeks@googlegroups.com.
>>>>> > To unsubscribe from this group, send email to
>>>>> > algogeeks+unsubscr...@googlegroups.com.
>>>>> > For more options, visit this group at
>>>>> > http://groups.google.com/group/algogeeks?hl=en.
>>>>> >
>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Ratan Kumar
>>>>> B. Tech
>>>>> MNNIT,
>>>>> Allahabad
>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "Algorithm Geeks" group.
>>>>> To post to this group, send email to algogeeks@googlegroups.com.
>>>>> To unsubscribe from this group, send email to
>>>>> algogeeks+unsubscr...@googlegroups.com.
>>>>> For more options, visit this group at
>>>>> http://groups.google.com/group/algogeeks?hl=en.
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Dheeraj Sharma
>>>> Comp Engg.
>>>> NIT Kurukshetra
>>>>
>>>>
>>>
>>>
>>>
>>> --
>>> Dheeraj Sharma
>>> Comp Engg.
>>> NIT Kurukshetra
>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "Algorithm Geeks" group.
>>> To post to this group, send email to algogeeks@googlegroups.com.
>>> To unsubscribe from this group, send email to
>>> algogeeks+unsubscr...@googlegroups.com.
>>> For more options, visit this group at
>>> http://groups.google.com/group/algogeeks?hl=en.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to algogeeks@googlegroups.com.
>> To unsubscribe from this group, send email to
>> algogeeks+unsubscr...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>
>
>
> --
>
> Regards
> BharatKumar Bagana
> http://www.google.com/profiles/bagana.bharatkumar
> Mobile +91 8056127652
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>



-- 
Ratan Kumar
B. Tech
MNNIT,
Allahabad

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to