okaysh
BAN USERneed an linear array of size 2*n*n
if n is even, merge first and second row in the beginning of the array, next take the merge portion and merge it into the end of the array and so on, finally you get the entire array merged from the beginning. If n is odd, start by merging in the end of the array.
#include<stdio.h>
#include<stdlib.h>
typedef struct tree
{
int a;
struct tree* left;
struct tree* right;
}TREE;
TREE* addNode(TREE* node, int val)
{
if(node==NULL)
{
node=(TREE*)malloc(sizeof(TREE));
node->a=val;
node->left=NULL;
node->right=NULL;
}
else if(val<=node->a)
node->left=addNode(node->left,val);
else if(val>node->a)
node->right=addNode(node->right,val);
return node;
}
void printInorder(TREE* head)
{
if(head==NULL)
return;
printInorder(head->left);
printf(" %d ",head->a);
printInorder(head->right);
}
int sum;
void printSum(TREE* head,int min,int max)
{
if(head==NULL)
return;
printSum(head->left,min,max);
if(head->a>min&&head->a<max)
sum+=head->a;
printSum(head->right,min,max);
}
int main()
{
int arr[]={2,5,9,7,6,-1,0,10,23,12,15,18,14,3,1};
int i;
TREE* head=NULL;
for(i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
head=addNode(head,arr[i]);
printInorder(head);
printf("\n");
int min=5,max=11;
printSum(head,min,max);
printf("%d\n",sum);
return 0;
}
#include<stdio.h>
int main(void)
{
int row=4;
int col=4;
int i=0,j=0,k=0;
int down=0,right=0,up=0,left=0;
int a[row][col];
int rmax,cmax;
int rmin,cmin;
int left_ind=-1;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
a[i][j]=10*i+j;
printf("Array\n");
for(i=0;i<row;i++){
for(j=0;j<col;j++)
printf("%3d ",a[i][j]);
printf("\n");
}
i=0;j=0;
rmax=row;cmax=col;
rmin=-1;cmin=0;
down=1;
for(k=0;k<row*col;k++){
if(left){
printf("%3d",a[i][j--]);
if(j==cmin){
left=0;
down=1;
cmin++;
j++;i++;
left_ind=k;
}
}
if(up){
printf("%3d",a[i--][j]);
if(i==rmin){
up=0;
left=1;
rmin++;
i++;j--;
}
}
if(right){
printf("%3d",a[i][j++]);
if(j==cmax){
right=0;
up=1;
cmax--;
j--;i--;
}
}
if(down && k!=left_ind){
printf("%3d",a[i++][j]);
if(i==rmax){
down=0;
right=1;
rmax--;
i--;j++;
}
}
}
printf("\n");
return 0;
}
1) qsort the array
2) if atleast the 0th element is negative , proceed else exit
3) take sum of the first and last element, if more positive, try to find the 3rd from second element, until a positive number found
if more negative, try searching from the second last element, till a negative number is found
4)if 3 fails, depending on more positive or negative, either change the first element to second or last to second last and perform step 3
#include<stdio.h>
#include<stdlib.h>
char* func(char* d)
{
int i=0,j=0;
int count=0;
char tmp[1000];
char tt[100];
char* ans;
tmp[i]=*d;
++count;d++;
while(0==0)
{
if(*d==tmp[i] && *d!='\0')
{
++count;
}
else
{
do
{
tt[j++]=(count%10)+'0';
}while((count=count/10)>0);
while(j--)
{
tmp[++i]=tt[j];
}
tmp[++i]=*d;
if(*d=='\0')
break;
j=0;
count=1;
}
d++;
}
tmp[++i]='\0';
ans=(char*)calloc(i,sizeof(char));
i=0;j=0;
while((ans[j++]=tmp[i++])!='\0');
return ans;
}
int main()
{
char *data="aaaabbbbbbbcdeeeffffffffggg";
char *cpr;
cpr=func(data);
printf("%s\n",cpr);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
char c;
struct node* next;
struct node* prev;
};
struct node* addnode(struct node* head,char data)
{
if(!head)
{
head=(struct node*)malloc(sizeof(struct node));
if(!head)
return NULL;
head->c=data;
head->next=head;
head->prev=head;
}
else
{
struct node* tmp=head;
struct node* tmp1=head;
struct node* newn;
while(tmp->next!=tmp1) tmp=tmp->next;
newn=(struct node*)malloc(sizeof(struct node));
if(!newn)
return NULL;
tmp->next=newn;
newn->c=data;
newn->next=tmp1;
newn->prev=tmp;
tmp1->prev=newn;
}
return head;
}
void printnode(struct node* head)
{
struct node* tmp=head;
struct node* tmp1=head;
do
{
printf("%c->",tmp->c);
tmp=tmp->next;
}while(tmp!=tmp1);
tmp=head;
tmp1=head;
do
{
printf("%c->",tmp->c);
tmp=tmp->prev;
}while(tmp!=tmp1);
printf("\n");
}
int main()
{
char* name="abcd";
struct node* head=NULL;
while(*name!='\0')
{
head=addnode(head,*name);
name++;
}
//printnode(head);
struct node* t1=head;
struct node* t2=head;
struct node* t3=head;
do
{
do
{
do
{
printf("%c",t2->c);
t2=t2->next;
}while(t2!=t3);
printf(",");
t2=t1;
t3=t3->prev;
}while(t3!=t1->next);
t3=t1;
do
{
do
{
printf("%c",t2->c);
t2=t2->prev;
}while(t2!=t3);
printf(",");
t2=t1;
t3=t3->next;
}while(t3!=t1);
t1=t1->next;
t2=t1;
t3=t1;
}while(t1!=head);
printf("\n");
return 0;
}
#include<stdio.h>
int main()
{
char data[]="1000101110001000";
int len=0;
int i,j;
for(;data[len]!='\0';len++);
i=0;
j=len-1;
do
{
while(data[i]!='1') i++;
while(data[j]!='0') j--;
if(i<=j)
{
data[i++]='0';
data[j--]='1';
}
}while(i<=j);
printf("%s\n",data);
return 0;
}
#include<stdio.h>
struct results
{
unsigned int idx;
unsigned int cnt;
};
char* arr="ab1234567890apqqrstppsxaabcdefgh";
int main()
{
unsigned int idx1,idx2,idx3;
unsigned int count,countb;
struct results data;
idx1=idx2=idx3=0;
count=1;
data.idx=0;
data.cnt=1;
printf("%s\n",arr);
while(arr[idx3++]!='\0')
{
for(;idx2<idx3;)
{
if(arr[idx2++]!=arr[idx3])
{
++count;
}
else
{
if(count>data.cnt)
{
data.cnt=count;
data.idx=idx1;
}
idx1=idx2;
break;
}
}
countb=count-1;
count=(idx2==idx3)?1:(idx3-idx1);
idx2=idx1;
}
if(countb>data.cnt)
{
data.cnt=countb;
data.idx=idx1;
}
printf("index=%u,count=%u\n",data.idx,data.cnt);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
struct rec
{
int num;
unsigned int count;
};
int arr[]={2,4,2,1,0,4,4,5,5,4,4,0,2};
void qsortX(int*a,int low,int high)
{
int i,j;
int key;
int mid=(low+high)/2;
key=a[mid];
i=low;j=high;
do
{
while(a[i]<key)i++;
while(a[j]>key)j--;
if(i<=j)
{
int tmp=a[i];
a[i]=a[j];
a[j]=tmp;
i++;j--;
}
}while(i<=j);
if(i<high) qsortX(a,i,high);
if(j>low) qsortX(a,0,j);
return;
}
void sort(int* a, int l)
{
qsortX(a,0,l-1);
return;
}
void qsortS(struct rec* a,int low,int high)
{
int i,j;
int key;
int mid=(low+high)/2;
key=a[mid].count;
i=low;j=high;
do
{
while(a[i].count>key)i++;
while(a[j].count<key)j--;
if(i<=j)
{
int tmp=a[i].count;
a[i].count=a[j].count;
a[j].count=tmp;
tmp=a[i].num;
a[i].num=a[j].num;
a[j].num=tmp;
i++;j--;
}
}while(i<=j);
if(i<high) qsortS(a,i,high);
if(j>low) qsortS(a,0,j);
return;
}
void sortS(struct rec* a, int l)
{
qsortS(a,0,l-1);
return;
}
void printarr(int* a,int l)
{
int i;
for(i=0;i<l;i++)
{
printf(" %d ",a[i]);
}
printf("\n");
return;
}
int main()
{
int len=sizeof(arr)/sizeof(arr[0]);
sort(arr,len);
printarr(arr,len);
int i,j;
int n=4;
struct rec* rec_arr;
rec_arr=(struct rec*)calloc(n,sizeof(struct rec));
if(!rec_arr)
return -1;
j=0;
rec_arr[j].num=arr[0];
rec_arr[j].count=1;
int last=arr[0];
for(i=1;i<len;i++)
{
if( arr[i]!=last)
{
rec_arr[++j].num=arr[i];
rec_arr[j].count=1;
last=arr[i];
}
else
{
rec_arr[j].count++;
}
}
if(j>=n)
{
sortS(rec_arr,j);
printf("%d occurence is of number %d %d times\n",n,rec_arr[n-1].num,rec_arr[n-1].count);
}
else
printf("the number given dosen't exist\n");
return 0;
}
#include<stdio.h>
int a[]={8,-9,11,-4,31,-1,2,-3,10,1,-3,4,7,-12,34,-12,-7,19};
int main()
{
int i,j;
int sum1,sum2;
int len;
len=sizeof(a)/sizeof(a[0]);
sum1=a[0]+a[1];
if(sum1==0)
{
printf("low=0 high=1\n");
return 0;
}
for(i=2;i<len;i++)
{
sum2=0;
sum1+=a[i];
if(sum1==0)
{
printf("low=0 high=%d\n",i);
return 0;
}
else
{
for(j=0;j<i;j++)
{
sum2+=a[j];
if(sum1-sum2==0)
{
printf("low=%d high=%d\n",j+1,i);
return 0;
}
}
}
}
printf("not found\n");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
char arr[5][5]={
{'A','C','P','R','C'},
{'X','S','O','P','C'},
{'V','O','V','N','I'},
{'W','G','F','M','N'},
{'Q','A','T','I','T'}
};
char* pattern="MICROSOFT";
int x,y;
int check_pattern(int i,int j)
{
int mx,my;
int len;
char *p;
int broken;
int found;
int i1;
char** store=(char**)calloc(x,sizeof(char*));
for(i1=0;i1<x;i1++)
{
store[i1]=(char*)calloc(y,sizeof(char));
}
store[i][j]=1;
for(len=0,p=pattern;*p++!='\0';len++);
p=pattern;broken=0;
len--;
while(len--&&!broken)
{
found=0;
char toMatch=*++p;
for(mx=-1;mx<=1;mx++)
{
for(my=-1;my<=1;my++)
{
if((i+mx)<0||(i+mx)>=x)
break;
if((j+my<0)||(j+my)>=y)
continue;
if(store[i+mx][j+my])
continue;
if(toMatch==arr[i+mx][j+my])
{
i=i+mx;j=j+my;
store[i][j]=1;
found=1;
printf("%c %d\n",arr[i][j],found);
break;
}
}
if(found)
break;
}
if(!found)
broken=1;
}
for(i1=0;i1<x;i1++)
{
free(store[i1]);
}
return broken?-1:0;
}
int main()
{
y=sizeof(arr[0])/sizeof(arr[0][0]);
x=sizeof(arr)/(y*sizeof(char));
int i,j;
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
if(arr[i][j]==*pattern)
{
if(0==check_pattern(i,j))
{
printf("pattern found\n");
return 0;
}
}
}
}
printf("pattern not found\n");
return -1;
}
#include<stdio.h>
#include<unistd.h>
unsigned int var=172795;
int main()
{
unsigned int sec=0;
unsigned int min=0;
unsigned int hr=0;
while(1)
{
++var;
sleep(1);
sec=var%60;
min=((var/60)>=60)?((var/60)%60):(var/60);
hr=((var/3600)>=24)?((var/3600)%24):(var/3600);
printf("%u:%u:%u\n",hr,min,sec);
}
return 0;
}
- okaysh June 20, 2012
number system with base 26
- okaysh October 07, 2012