how abt dis guys ??
#include <stdio.h>
#include <string.h>
#define MAX 100
int main()
{
int n;
int i;
int j;
int it;
char input[MAX];
char tmp;
scanf("%s",input);
n = strlen(input);
i = j = n/2;
for(it=1; it<n-2; it++) {
if(it%2 == 1) {
tmp = input[j];
input[j] = input[it];
input[it] = tmp;
j++;
} else if( it < i ) {
tmp = input[i];
input[i] = input[it];
input[it] = tmp;
} else {
tmp = input[it];
input[it] = input[it+1];
input[it+1] = tmp;
}
}
printf("\n%s\n",input);
return 0;
}
On Mon, Feb 28, 2011 at 8:29 PM, Arpit Sood <[email protected]> wrote:
> Are there any constraints in the problem, because it seems straight
> forward.
>
> if number of elements are 2n indexed from 0 to 2n-1
>
> for i=0 to n-1:
> new_array[i*2]=old_array[i];
> new_array[i*2+1]=old_array[i+n];
>
> On Mon, Feb 28, 2011 at 7:41 PM, bittu <[email protected]> wrote:
>
>> @jalaj U needs to clarify becoz what i can say that dat is overwritten
>> in ur explanation so we loosing the original data where we are saving
>> when we swapping the elements ur explanation seems to be right but
>> little confusing
>>
>> @ujjwal i haven't tested ur code but i think its O(n^2) then why not
>> try for this 20 Line Simple code of O(n^2)
>>
>> Finally we wants O(n) better solution fro this which exist for
>> this ???
>>
>> #include<string.h>
>>
>> void swap(char *c,char *p)
>> {
>> char tmp;
>> tmp=*c;
>> *c=*p;
>> *p=tmp;
>>
>> }
>>
>> int main (int argc, char const* argv[])
>> {
>> char str[] = "1234abcd";
>> int i,j;
>> int len = strlen(str)/2;
>> //swap str[len-1] and str[len] and so on
>> for ( i = 0; i < len-1; i += 1) {
>> for ( j = len-1-i; j <= len+i; j += 2)
>> {
>> printf("i=%d j=%d c1=%c \t c2=%c
>> \n",i,j,str[j],str[j+1]);
>> swap(&str[j],&str[j+1]);
>> }
>> }
>> printf("%s \n", str);
>> return 0;
>> }
>>
>> Time Complexcity O(n^2) O(n) Needed..????
>>
>>
>> Thanks & Regards
>> Shashank >> "The Best Way to escape From The Problem is Solve It"
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> 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 [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> 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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.