ananthakrishnan.s.r
BAN USERYes, valid point. It can be done like this,
n=arr.size();
n=n/2+n%2;
for (int i = 0; i < n; i++) {
arr.remove(i);
}
Is it perfect now?
for (i = 0; i < arr.size(); i++) {
arr.remove(i);
}
Hi,
1.Sort the 3 arrays A,B,C.
2.Have 3 pointers a,b,c for 3 arrays.
3.Point each pointer to the beginning of each array.[a=>A,b=>B,c=>C]
4.Take the difference between MAX-MIN value of those current three values.
5.If the difference is minimum than current minimum_difference then update current minimum difference current a,b,c.
6.Then, move the pointer with minimum value, since we are trying to minimize the difference.
7.repeat these steps till end of array is reached for atleast one array.
Please let me know if you find some difficulties with my explanation.
check this
preordermorrisiterative(Tnode *root) {
Tnode *current = root, *pred = NULL, *succesor = NULL;
while (current != NULL) {
if (current->left == NULL) {
printf(" %d ", current->data);
current = current->right;
} else {
pred = current->left;
while (pred->right != NULL && pred->right != current) {
pred = pred->right;
}
if (pred->right == NULL) {
pred->right = current;
//preorder
printf(" %d ", current->data);
current = current->left;
} else {
pred->right = NULL;
//inorder
//printf(" %d ", current->data);
current = current->right;
}
}
}
}
check this
int TreeDiameter(Tnode *root)
{
static int max,diameter;
static Tnode *tmp=NULL;
if(root==NULL)
return 0;
if(tmp==NULL)
tmp=root;
int leftmax=TreeDiameter(root->left);
int rightmax=TreeDiameter(root->right);
if(diameter<(leftmax+rightmax))
diameter=leftmax+rightmax;
if(leftmax >= rightmax)
{
max=leftmax;
}
else
{
max=rightmax;
}
if(tmp==root)
return diameter;
return 1+max;
}
Check this :
int findmindistanceab(int A[], int n, int a, int b) {
int i = 0,val=0,min=9999,curr=-1,index[2];
for (i = 0; i < n; i++) {
if (A[i] == a)
val = 0;
else if (A[i] == b)
val = 1;
else
continue;
if (curr == (!val) && (i - index[curr] - 1) < min)
min = i - index[curr] - 1;
curr = val;
index[curr] = i;
}
return min;
}
int main(int argc, char** argv) {
int a[]={7,5,5,2,1,6,3,3,2,1,5,5,1,2,3,10};
int n=sizeof(a)/sizeof(int);
printf(" %d ",findmindistanceab(a,n,3,5));
return (EXIT_SUCCESS);
}
Check this:
typedef struct mindif{
int mindiff;
int i;
int j;
int k;
}mindif;
mindif* findijk(int a[],int b[],int c[],int I,int J,int K)
{
int i=0,j=0,k=0,diff=0,mindiff=99999,min=0,max=0,mindiffI=-1,mindiffJ=-1,mindiffK=-1,minI,minJ,minK;
while(i<I && j<J && k<K)
{
minI=i;
minJ=j;
minK=k;
if(a[i]<b[j] && a[i]<c[k])
{
min=a[i];
i++;
if(b[j]>c[k])
{
max=b[j];
}
else
{
max=c[k];
}
}
else if(b[j]<c[k])
{
min=b[j];
j++;
if(c[k]>a[i])
{
max=c[k];
}
else
{
max=a[i];
}
}
else
{
min=c[k];
k++;
if(a[i]>b[j])
{
max=a[i];
}
else
{
max=b[j];
}
}
diff=max-min;
if(diff<mindiff)
{
mindiff=diff;
mindiffI=minI;
mindiffJ=minJ;
mindiffK=minK;
}
}
mindif *smin =(mindif*)malloc(sizeof(mindif));
smin->mindiff=mindiff;
smin->i=mindiffI;
smin->j=mindiffJ;
smin->k=mindiffK;
return smin;
//printf("min diff = %d i=%d j=%d k=%d",mindiff,mindiffI,mindiffJ,mindiffK);
}
int main(int argc, char** argv) {
int a[]={10,15,25,30};
int b[]={17,19,26};
int c[]={14,21,35};
mindif *smin=findijk(a,b,c,4,3,3);
printf("min diff = %d i=%d j=%d k=%d",smin->mindiff,smin->i,smin->j,smin->k);
return (EXIT_SUCCESS);
}
Idea is like this since both the arrays may not be of same length lets divide (k-1) smallest elements proportionally in both the arrays:
let i point the array A by
i=m/(m+n) * (k-1) [since we have to divide k-1 elements among two]
j=(k-1) - i
then try to insert A[i] between B[j-1] and B[j] if three are not in asc order try to insert B[j] between A[i-1] and A[i]
If any one of the above satisfies we found kth smallest element else,
check which one is smallest among A[i] and B[j] its logical that if A[i] is smallest then we can A[0] to A[i] for the next iteration and
k becomes k-i-1 also m becomes m-i-1 i.e now we have only m-i-1+n elements out of which we have to find k-i-1th smallest thus the iteration goes on until we
find our kth smallest element.
Consider 2 arrays
A={5,7,9,20}; length of A: m=4
B={10,12,21,27,35,50}; length of B: n=6
let K be 4
i=4/10*3=1; A[1]=7;
j=3-1=2; B[2]=21;
B[1]=12 A[1]=7 B[2]=21 [not in asc order]
A[0]=5 B[2]=21 A[1]=7 [not in asc order]
so now,
k=k-i-1 =4-1-1=2
m=m-i-1=4-1-1=2
n=6
A={9,20}; length of A: m=2
B={10,12,21,27,35,50}; length of B: n=6
i=2/8*1=0; A[0]=9;
j=1-0=1; B[1]=12;
(acutally A[-1] is just for understanding)
B[0]=10 A[0]=9 B[1]=12 [not in asc order]
A[-1]=-INF B[1]=12 A[0]=9 [not in asc order]
now,
k=k-i-1=2-0-1=1;
m=m-i-1=2-0-1=1;
n=6;
A={20}; length of A: m=1
B={10,12,21,27,35,50}; length of B: n=6
i=1/7*0=0; A[0]=20;
j=0-0=0; B[0]=10;
(acutally A[-1] and B[-1] are just for understanding)
B[-1]=-INF A[0]=20 B[0]=10 [not in asc order]
A[-1]=-INF B[0]=10 A[0]=20 [in asc order]
We got the Kth(4th) smallest element which is 10.
what is your problem?
- ananthakrishnan.s.r June 27, 2011Welcome :)
running time is O(2*n) and not O(n^2)
In First pass it will check for 0 and -1.
In Second pass it will check for 1.
Thats it.
Check my code
int FindXYZ(Tnode *root,Tnode *X,Tnode *Y,Tnode *Z)
{
if(root==NULL)
return 0;
static Tnode *rootnode=NULL;
static int found=0;
if(rootnode==NULL)
rootnode=root;
int left=FindXYZ(root->left,X,Y,Z);
int right=FindXYZ(root->right,X,Y,Z);
if(left==1 && right==1 && found==0)
{
if(root==Y)
found=1;
}
if((left==11 || right==11)&&found==0)
{
if(root==X || root==Z)
found=1;
}
if(left==10 || right==10)
found=-1;
if(left==2 || right==2)
found=-1;
if(rootnode==root)
return found>0;
if(root==X||root==Z)
return left+right+1;
if(root==Y)
return left+right+10;
return left+right;
}
Check this:
int multiply(int a[],int n,int nextproduct,int i)
{
int prevproduct=1;
if(i>=n)
return prevproduct;
prevproduct=multiply(a,n,nextproduct*a[i],i+1);
printf(" i=%d > %d\n",i,prevproduct*nextproduct);
return prevproduct*a[i];
}
int main()
{
int a[]={2,4,1,3,5};
multiply(a,5,1,0);
return 0;
}
@master fuji
How do you say "no solution".
The output is correct only, please read the question again.
Hi All,
Please check this code
http ideone.com/YF9qi
Can we do better than this?
int ispalindrome(int x)
{
int n=x,y=0;
while(n)
{
y=(y<<1) | (n&1);
n = n>>1;
}
return x==y;
}
int main()
{
int a=3;
printf(ispalindrome(a)==1?"Palindrome.":"Not a palindrome.");
}
Please check my code:
typedef struct node
{
char data;
struct node *left;
struct node *right;
}node;
node* constructTreeNL(node *root,char *input)
{
if(*input == '\0')
return;
while(*input)
{
if(root->left&&root->right)
return root;
if(*input == 'n')
{
if(root->left == NULL)
{
node *newnode=(node*)malloc(sizeof(node));
newnode->data='n';
newnode->left=NULL;
newnode->right=NULL;
root->left=newnode;
input++;
constructTreeNL(root->left,input);
}
else if(root->right == NULL)
{
node *newnode=(node*)malloc(sizeof(node));
newnode->data='n';
newnode->left=NULL;
newnode->right=NULL;
root->right=newnode;
input++;
constructTreeNL(root->right,input);
}
}
else if(*input == 'l')
{
if(root->left == NULL)
{
node *newnode=(node*)malloc(sizeof(node));
newnode->data='l';
newnode->left=NULL;
newnode->right=NULL;
root->left=newnode;
}
else if(root->right == NULL)
{
node *newnode=(node*)malloc(sizeof(node));
newnode->data='l';
newnode->left=NULL;
newnode->right=NULL;
root->right=newnode;
}
}
input++;
}
return root;
}
node* constructNL(char* input)
{
if(input == NULL || *input == '\0')
return;
node *virtualroot=(node*)malloc(sizeof(node));
virtualroot->data = 0;
virtualroot->left = NULL;
virtualroot->right = NULL;
node *tmproot=constructTreeNL(virtualroot,input);
if(tmproot->left == NULL)
return;
return tmproot->left;
}
void preorder(node *root)
{
if(root == NULL)
return;
printf("%c",root->data);
if(root->left)
preorder(root->left);
if(root->right)
preorder(root->right);
}
int main()
{
preorder(constructNL("nnlll"));
}
Please check my code:
int ispalindromeEFF(node* head)
{
if(head==NULL)
{
return 0;
}
node *xq,*y,*p,*r;
int flag=1;
p=NULL;
xq=head;
r=xq->link;
y=head;
while(y->link && y->link->link)
{
y=y->link->link;
//reverse
xq->link=p;
p=xq;
xq=r;
if(r)
{
r=r->link;
}
}
if(y->link) //even length
{
xq->link=p;
p=xq;
xq=r;
r=r->link;
y=xq;
}
else
{
y=r;
}
node *temp;
temp=p;
p=xq;
xq=temp;
if(xq)
{
r=xq->link;
}
while(y)
{
if(y->data != xq->data)
{
flag=0;
}
y=y->link;
xq->link=p;
p=xq;
xq=r;
if(r)
{
r=r->link;
}
}
return flag;
}
Feedbacks are welcome
- ananthakrishnan.s.r June 20, 2011Please check my code:
#include<stdio.h>
void findabc(int ArrA[],int x,int ArrB[],int y,int ArrC[],int z)
{
int mindiff=99999;
heapsort(ArrA,x);
heapsort(ArrB,y);
heapsort(ArrC,z);
int *A=ArrA,*B=ArrB,*C=ArrC,a,b,c,diff,diffa,diffb,diffc,min=0,max=99999;
while(A<ArrA+x && B<ArrB+y && C<ArrC+z)
{
a=*A;b=*B;c=*C;
if(a>b && a>c)
{
max=a;
if(b<c)
{
min=b;
B++;
}
else
{
min=c;
C++;
}
}
else if(b>c)
{
max=b;
if(a<c)
{
min=a;
A++;
}
else
{
min=c;
C++;
}
}
else
{
max=c;
if(a<b)
{
min=a;
A++;
}
else
{
min=b;
B++;
}
}
diff=max - min;
if(diff < mindiff)
{
mindiff=diff;
diffa=a;
diffb=b;
diffc=c;
}
}
printf(" MIN DIFF=%d a=%d b=%d c=%d\n",mindiff,diffa,diffb,diffc);
}
int main()
{
int ArrA[]={10,12,21,30};
int ArrB[]={17,25,35};
int ArrC[]={27,28,29,32};
int x=4,y=3,z=4;
findabc(ArrA,x,ArrB,y,ArrC,z);
}
Feedbacks are welcome
- ananthakrishnan.s.r June 20, 2011Please check my code
#include<stdio.h>
int findKthsmallest(int a[],int m,int b[],int n,int k)
{
int i=0,j=0,ti=0,tj=0,I=0,J=0,M=m,N=n;
while(1)
{
ti = (int)((double)m/(m+n) * (k-1));
tj = (k-1)-ti;
i = I+ti;
j= J+tj;
//printf(" i=%d j=%d\n",i,j);
if(j>0 && j<N && i<M && a[i]>b[j-1] && a[i]<b[j])
return a[i];
if(i>0 && i<M && j<N && b[j]>a[i-1] && b[j]<a[i])
return b[j];
if(j==0 && i<M && a[i]<b[j])
return a[i];
if(i==0 && j<N && b[j]<a[i])
return b[j];
if(j==N && a[i]>b[j-1])
return a[i];
if(i==M && b[j]>a[i-1])
return b[j];
if(i<M && j<N)
{
if(a[i]<b[j])
{
k=k-ti-1;
m=m-ti-1;
I=i+1;
}
else
{
k=k-tj-1;
n=n-tj-1;
J=j+1;
}
}
else if(i>=M)
{
k=k-tj-1;
n=n-tj-1;
J=j+1;
}
else
{
k=k-ti-1;
m=m-ti-1;
I=i+1;
}
}
}
int main()
{
int a[]={1,2,3};
int b[]={4};
int m=3,n=1,k=3;
printf("%d",findKthsmallest(a,m,b,n,k));
return 0;
}
please check my code:
#include<stdio.h>
int findKthsmallest(int a[],int m,int b[],int n,int k)
{
int i=0,j=0,ti=0,tj=0,I=0,J=0,M=m,N=n;
while(1)
{
ti = (int)((double)m/(m+n) * (k-1));
tj = (k-1)-ti;
i = I+ti;
j= J+tj;
//printf(" i=%d j=%d\n",i,j);
if(j>0 && j<N && i<M && a[i]>b[j-1] && a[i]<b[j])
return a[i];
if(i>0 && i<M && j<N && b[j]>a[i-1] && b[j]<a[i])
return b[j];
if(j==0 && i<M && a[i]<b[j])
return a[i];
if(i==0 && j<N && b[j]<a[i])
return b[j];
if(j==N && a[i]>b[j-1])
return a[i];
if(i==M && b[j]>a[i-1])
return b[j];
if(i<M && j<N)
{
if(a[i]<b[j])
{
k=k-ti-1;
m=m-ti-1;
I=i+1;
}
else
{
k=k-tj-1;
n=n-tj-1;
J=j+1;
}
}
else if(i>=M)
{
k=k-tj-1;
n=n-tj-1;
J=j+1;
}
else
{
k=k-ti-1;
m=m-ti-1;
I=i+1;
}
}
}
int main()
{
int a[]={1,2,3};
int b[]={4};
int m=3,n=1,k=3;
printf("%d",findKthsmallest(a,m,b,n,k));
return 0;
}
Please check my code:
#include<stdio.h>
int findKthsmallest(int a[],int m,int b[],int n,int k)
{
int i=0,j=0,ti=0,tj=0,I=0,J=0,M=m,N=n;
while(1)
{
ti = (int)((double)m/(m+n) * (k-1));
tj = (k-1)-ti;
i = I+ti;
j= J+tj;
//printf(" i=%d j=%d\n",i,j);
if(j>0 && j<N && i<M && a[i]>b[j-1] && a[i]<b[j])
return a[i];
if(i>0 && i<M && j<N && b[j]>a[i-1] && b[j]<a[i])
return b[j];
if(j==0 && i<M && a[i]<b[j])
return a[i];
if(i==0 && j<N && b[j]<a[i])
return b[j];
if(j==N && a[i]>b[j-1])
return a[i];
if(i==M && b[j]>a[i-1])
return b[j];
if(i<M && j<N)
{
if(a[i]<b[j])
{
k=k-ti-1;
m=m-ti-1;
I=i+1;
}
else
{
k=k-tj-1;
n=n-tj-1;
J=j+1;
}
}
else if(i>=M)
{
k=k-tj-1;
n=n-tj-1;
J=j+1;
}
else
{
k=k-ti-1;
m=m-ti-1;
I=i+1;
}
}
}
int main()
{
int a[]={1,2,3};
int b[]={4};
int m=3,n=1,k=3;
printf("%d",findKthsmallest(a,m,b,n,k));
return 0;
}
Please check my code
#include<stdio.h>
int findKthsmallest(int a[],int m,int b[],int n,int k)
{
int i=0,j=0,ti=0,tj=0,I=0,J=0,M=m,N=n;
while(1)
{
ti = (int)((double)m/(m+n) * (k-1));
tj = (k-1)-ti;
i = I+ti;
j= J+tj;
//printf(" i=%d j=%d\n",i,j);
if(j>0 && j<N && i<M && a[i]>b[j-1] && a[i]<b[j])
return a[i];
if(i>0 && i<M && j<N && b[j]>a[i-1] && b[j]<a[i])
return b[j];
if(j==0 && i<M && a[i]<b[j])
return a[i];
if(i==0 && j<N && b[j]<a[i])
return b[j];
if(j==N && a[i]>b[j-1])
return a[i];
if(i==M && b[j]>a[i-1])
return b[j];
if(i<M && j<N)
{
if(a[i]<b[j])
{
k=k-ti-1;
m=m-ti-1;
I=i+1;
}
else
{
k=k-tj-1;
n=n-tj-1;
J=j+1;
}
}
else if(i>=M)
{
k=k-tj-1;
n=n-tj-1;
J=j+1;
}
else
{
k=k-ti-1;
m=m-ti-1;
I=i+1;
}
}
}
int main()
{
int a[]={1,2,3};
int b[]={4};
int m=3,n=1,k=3;
printf("%d",findKthsmallest(a,m,b,n,k));
return 0;
}
Feedbacks are welcome
- ananthakrishnan.s.r June 16, 2011Please check my code
#include<stdio.h>
int findKthsmallest(int a[],int m,int b[],int n,int k)
{
int i=0,j=0,ti=0,tj=0,I=0,J=0,M=m,N=n;
while(1)
{
ti = (int)((double)m/(m+n) * (k-1));
tj = (k-1)-ti;
i = I+ti;
j= J+tj;
//printf(" i=%d j=%d\n",i,j);
if(j>0 && j<N && i<M && a[i]>b[j-1] && a[i]<b[j])
return a[i];
if(i>0 && i<M && j<N && b[j]>a[i-1] && b[j]<a[i])
return b[j];
if(j==0 && i<M && a[i]<b[j])
return a[i];
if(i==0 && j<N && b[j]<a[i])
return b[j];
if(j==N && a[i]>b[j-1])
return a[i];
if(i==M && b[j]>a[i-1])
return b[j];
if(i<M && j<N)
{
if(a[i]<b[j])
{
k=k-ti-1;
m=m-ti-1;
I=i+1;
}
else
{
k=k-tj-1;
n=n-tj-1;
J=j+1;
}
}
else if(i>=M)
{
k=k-tj-1;
n=n-tj-1;
J=j+1;
}
else
{
k=k-ti-1;
m=m-ti-1;
I=i+1;
}
}
}
int main()
{
int a[]={1,2,3};
int b[]={4};
int m=3,n=1,k=3;
printf("%d",findKthsmallest(a,m,b,n,k));
return 0;
}
Comments are welcome
- ananthakrishnan.s.r June 16, 2011Please check my code
#include<stdio.h>
int findKthsmallest(int a[],int m,int b[],int n,int k)
{
int i=0,j=0,ti=0,tj=0,I=0,J=0,M=m,N=n;
while(1)
{
ti = (int)((double)m/(m+n) * (k-1));
tj = (k-1)-ti;
i = I+ti;
j= J+tj;
//printf(" i=%d j=%d\n",i,j);
if(j>0 && j<N && i<M && a[i]>b[j-1] && a[i]<b[j])
return a[i];
if(i>0 && i<M && j<N && b[j]>a[i-1] && b[j]<a[i])
return b[j];
if(j==0 && i<M && a[i]<b[j])
return a[i];
if(i==0 && j<N && b[j]<a[i])
return b[j];
if(j==N && a[i]>b[j-1])
return a[i];
if(i==M && b[j]>a[i-1])
return b[j];
if(i<M && j<N)
{
if(a[i]<b[j])
{
k=k-ti-1;
m=m-ti-1;
I=i+1;
}
else
{
k=k-tj-1;
n=n-tj-1;
J=j+1;
}
}
else if(i>=M)
{
k=k-tj-1;
n=n-tj-1;
J=j+1;
}
else
{
k=k-ti-1;
m=m-ti-1;
I=i+1;
}
}
}
int main()
{
int a[]={1,2,3};
int b[]={4};
int m=3,n=1,k=3;
printf("%d",findKthsmallest(a,m,b,n,k));
return 0;
}
Comments are welcome
- ananthakrishnan.s.r June 16, 2011Please check my code:
#include<stdio.h>
int findmissing(int a[],int n)
{
int i=0,s=0;
for(i=0;i<n-1;i++)
{
s = s^a[i]^(i+1);
}
s = s^n;
return s;
}
int main()
{
int a[]={5,4,1,3};
printf(" %d ",findmissing(a,5));
return 0;
}
Feedbacks are welcome.
- ananthakrishnan.s.r June 10, 2011Please check my code:
#include<stdio.h>
typedef struct node{
char data;
struct node* link;
}node;
node* ispalindromic(node* n,node* head)
{
if(n == NULL)
return head;
head = ispalindromic(n->link,head);
if(head == NULL)
return NULL;
if(head >= n)
return head;
if(head->data != n->data)
return NULL;
return head->link;
}
int ispalindrome(node *n)
{
if(ispalindromic(n,n) == NULL)
{
printf(" NOT A PALINDROME.\n");
return -1;
}
printf("PALINDROME.\n");
return 1;
}
int main()
{
node *n,*head,*temp;
n=(node*)malloc(sizeof(node));
n->data='m';
head=n;
temp=n;
n=(node*)malloc(sizeof(node));
n->data='a';
temp->link=n;
temp=n;
n=(node*)malloc(sizeof(node));
n->data='d';
temp->link=n;
temp=n;
n=(node*)malloc(sizeof(node));
n->data='a';
temp->link=n;
temp=n;
n=(node*)malloc(sizeof(node));
n->data='m';
temp->link=n;
temp=n;
ispalindrome(head);
return 0;
}
Feedbacks are welcome.
- ananthakrishnan.s.r June 10, 2011Please check my code :
#include<stdio.h>
void printzigzag(int a[][5],int m,int n)
{
int i=0,j=0;
for(i=0;i<m;i++)
{
if(i%2==0)
{
for(j=0;j<n;j++)
{
printf(" %d ",a[i][j]);
}
}
else
{
for(j=n-1;j>=0;j--)
{
printf(" %d ",a[i][j]);
}
}
}
}
int main()
{
int a[][5]={{1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15},
{16,17,18,19,20}};
printzigzag(a,4,5);
return 0;
}
Feedbacks are welcome.
- ananthakrishnan.s.r June 10, 2011Please check my code
#include<stdio.h>
int mergeandcountinversion(int a[],int begin,int middle,int end,int z)
{
int length=end-begin+1;
int tmp[length];
int i1=begin,i2=middle+1,it=0,i=0;
for(i=0;i<length;i++)
{
if(i1>middle)
{
for(;i2<=end;i2++)
{
tmp[it]=a[i2];
it++;
}
break;
}
if(i2>end)
{
for(;i1<=middle;i1++)
{
tmp[it]=a[i1];
it++;
}
break;
}
if(a[i1]<=a[i2])
{
tmp[it]=a[i1];
it++;
i1++;
}
else if(a[i2]<a[i1])
{
tmp[it]=a[i2];
it++;
i2++;
z+=middle-i1+1;
}
}
i1=begin;
for(i=0;i<length;i++)
{
a[i1++]=tmp[i];
}
return z;
}
int m_sort(int a[],int begin,int end)
{
int middle=(begin+end)/2,x=0,y=0;
if(begin==end)
return 0;
x=m_sort(a,begin,middle);
y=m_sort(a,middle+1,end);
return x+y+mergeandcountinversion(a,begin,middle,end,0);
}
int mergesort(int a[],int n)
{
return m_sort(a,0,n-1);
}
int main()
{
int a[]={4,5,3,1,2};
int n=5,i=0;
printf("%d\n",mergesort(a,n));
}
Feedbacks are welcome
- ananthakrishnan.s.r June 09, 2011Please check my code:
#include<stdio.h>
int arrange(int a[],int b[],int c[],int n)
{
int i=0,ci=0;
for(i=0;i<n;i++)
{
if(b[i] == 0)
{
c[i]=a[i];
}
if(b[i] == -1)
{
while(c[ci]!=0 && ci<i)
{
ci++;
}
if(c[ci]!=0 || ci>=i)
{
return -1;
}
if(b[ci]!=0)
{
c[ci]=a[i];
ci++;
}
}
}
for(i=0;i<n;i++)
{
if(b[i] == 1)
{
while(c[ci]!=0 && ci<n)
{
ci++;
}
if(c[ci]!=0 || ci<=i)
{
return -1;
}
if(b[ci]!=0)
{
c[ci]=a[i];
ci++;
}
}
}
return 0;
}
int main()
{
int a[]={1,2,3};//{1,2,3,4,5,6,7,8};//{1,2,3,4};
int b[]={1,0,-1};//{1,1,0,-1,1,1,-1,-1};//{1,-1,0,1};
int c[]={0,0,0};//{0,0,0,0,0,0,0,0,0};//{0,0,0,0};
int i=0,n=3;
if(arrange(a,b,c,n)==-1)
{
printf("No Solution.\n");
}
else
{
for(i=0;i<n;i++)
{
printf(" %d ",c[i]);
}
}
return 0;
}
Feedbacks are welcome.
- ananthakrishnan.s.r June 09, 2011Please check this code
#include<stdio.h>
int compare(char *s1,char *s2)
{
if(s1 == NULL && s2 == NULL)
return 0;
if(s1 == NULL)
return -1;
if(s2 == NULL)
return 1;
while(*s1 && *s2)
{
if(*s1 < *s2)
return -1;
if(*s1 > *s2)
return 1;
s1++;
s2++;
}
if(*s1)
return 1;
if(*s2)
return -1;
return 0;
}
char *itoa(char *str,int a)
{
char *result=str,*start=str,*end;
int mod=0;
if(a<0)
{
*str = '-';
start++;
str++;
a=-a;
}
while(a >= 10)
{
mod = a%10;
a = a/10;
*str = 48 + mod;
str++;
}
*str = 48 + a;
end=str;
*(str+1)='\0';
while(start < end)
{
*start = *start ^ *end;
*end = *start ^ *end;
*start = *start ^ *end;
start++;
end--;
}
return result;
}
char* concat(char *s1,char *s2)
{
if(s1 == NULL && s2 == NULL)
return NULL;
if(s1 == NULL)
return s2;
if(s2 == NULL)
return s1;
char *result=s1;
while(*s1)
{
s1++;
}
while(*s2)
{
*s1=*s2;
s1++;
s2++;
}
*s1=*s2;
return result;
}
void sort(int a[],int n)
{
int i=0,j=0;
char *s1=(char*)malloc(1024);
char *s2=(char*)malloc(1024);
char *s3=(char*)malloc(1024);
char *s4=(char*)malloc(1024);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(compare( concat(itoa(s1,a[i]),itoa(s2,a[j])), concat(itoa(s3,a[j]),itoa(s4,a[i])) )==-1)
{
a[i] = a[i]^a[j];
a[j] = a[i]^a[j];
a[i] = a[i]^a[j];
}
}
}
}
int main()
{
int a[]={10,9};
int n=2,i=0;
sort(a,n);
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
return 0;
}
Feedbacks are welcome.
- ananthakrishnan.s.r June 08, 2011
Explain why do you think this logic is wrong. "MIN DIFF" is just for debugging, it never calculates |a-b|+|b-c|+|c-a|.
- ananthakrishnan.s.r March 21, 2012Good luck!