vino321
BAN USERAs For example, we will take the same number
Number = 34722641
Assuming a[0] =1
a[1] =4 and so on...
Compare a[i] & a[i+1] and loop through until a[i] < a[i+1]
i=0;
while( a[i] < a[i+1])
{
i++;
}
Once we come out of the loop, we know where a[i] > a[i+1]
so the number to swap is a[i+1] and the number which is between j=0 to i-1 and it should the smallest number which is greater than a[i+1]
so the below code does that ,
num = a[i+1];
j=0;
curMin = INT_MAX;//some random max number
while(j<i)
{
if( a[j] > num && a[j] <curMin)
{
curMin = a[j];
minIndex = j;
}
j++;
}
//Now swap a[i+1 ] and a[minIndex]
Now again we need to check whether the element at a[0] is even , it it is even then we have arrived at the result
else
search for i =0 to n , and swap the first even number with a[0].
Regards,
Vinodh
Definition of max is missing which is pretty obvious to write by us. Easy to Understand solution. Great job!
- vino321 April 12, 2014