brijesh kumar jaiswal
BAN USERi m simple that's enough.Software professional and Research Engg. in Cdot.
@Crab ,
Here u have assumed that array is sorted.
and ur alog return from function if two consecutive element is same.
XORing the all element of array. final result gives the odd repeating number.
time complexity is O(N).
oddnum=arr[0]
for(i=1;i<N;i++)
oddnum = oddnum ^ arr[i];
oddnum have odd repeating number.
if( S2 <S1 < E2 || S1< S2 < E1)
return success
int no_of_onbit(int val)
{
while(val)
{
val = val & (val-1);
count++;
}
return count;
}
#include<stdio.h>
#define MAX sizeof(int)
typedef union {
int val;
char str[MAX];
}testend;
main()
{
testend tend;
tend.val = 1;
if(tend.str[0] == 1)
printf("little endian");
else
printf("Big endian\n");
}
void show_n_elment(link *ln, int N)
{
link *nelement = ln, *node = ln;
int i = 0 ;
while(i < N && node != NULL)
{
i++;
node = node->next;
}
if(i != N)
{
printf("\n No. of element in link list is less than %d \n ",N);
return ;
}
else
{
while(node != NULL)
{
node = node->next;
nelement = nelement->next;
}
}
printf("\n %dth element of link list from end is : %d " ,N,nelement->info);
}
struct linklist
{
int info;
struct linklist *next;
};
typedef struct linklist link;
void add(link *first ,link *second)
{
int c = 0;
link *start , *node1,*tmp,*tmp1,*tmp2;
node1 = (link *)malloc(sizeof(link));
node1->info = (first->info + second->info + c)%10 ;
c = (first->info + second->info +c)/10;
node1->next = NULL;
start = node1;
tmp1 = first->next;
tmp2 = second->next;
while(tmp1 != NULL && tmp2 != NULL)
{
tmp = (link *)malloc(sizeof(link));
tmp->info = (tmp1->info + tmp2->info + c)%10 ;
c = (tmp1->info + tmp2->info + c)/10 ;
tmp->next = NULL;
node1->next = tmp;
node1 = tmp;
tmp1 = tmp1->next;
tmp2 = tmp2->next;
}
if(tmp1!=NULL)
{
while(tmp1 != NULL)
{
tmp = (link *)malloc(sizeof(link));
tmp->info = (tmp1->info + c)%10 ;
c = (tmp1->info + c)/10 ;
tmp->next = NULL;
node1->next = tmp;
node1 = tmp;
tmp1 = tmp1->next;
}
}
else
{
while(tmp2 != NULL)
{
tmp = (link *)malloc(sizeof(link));
tmp->info = (tmp2->info + c)%10 ;
c = (tmp2->info + c)/10 ;
tmp->next = NULL;
node1->next = tmp;
node1 = tmp;
tmp2 = tmp2->next;
}
}
if(c!=0)
{
tmp = (link *)malloc(sizeof(link));
tmp->info = c ;
tmp->next = NULL;
node1->next = tmp;
node1 = tmp;
}
//resulting link list have starting node at start
}
max_of_two_int(int a , int b)
{
int max[2];
int pos;
max[0] = a ;
max[1] = b;
pos = ((a-b) >> ((sizeof(int)*8 - 1))) & 1;
printf("\nmax = %d \n",max[pos] );
}
int BitSwapReqd(int A, int B)
{
unsigned int count;
int diffnum = A ^ B;
for(count=0; diffnum; count++){
diffnum &= diffnum-1;
}
// here count is total no. of bit
}
reversing string
here n is length of string
for(i = 0 ; i < n/2 ; i++)
{
str[i] = str[i] - str[n-1-i];
str[n-1-i] = str[n-1-i] + str[i] ;
str[i] = str[n-1-i] - str[i];
}
for swapping variable
num1 = num1 - num2 ;
num2 = num2 + num1 ;
num1 = num2 - num1;
for swapping variable
num1 = num1 ^ num2 ;
num2 = num1 ^ num2 ;
num1 = num1 ^ num2 ;
union testendien{
int num;
char byteval[4];
}check;
main()
{
check.num = 1;
(check.num & check.byteval[3])?printf("little endian"):printf("big endian");
}
#include <stdio.h>
#include <string.h>
#include <math.h>
void numtostring(int ,int , char str[]);
main()
{
unsigned int num ;
char str[10];
char constr[1000]="";
int i;
printf("\n enter the number which do u want to convert in string : ");
scanf("%ud " , &num);
i = sprintf(str , "%d", num);
numtostring(num ,i , constr);
printf("\n string of given number : %d \n %s \n", num , constr);
}
void numtostring(int num , int len, char str[])
{
char *arr1[] = { "","One ","Two ","Three ","Four ","Five ","Six ","Seven ","Eight ","Nine "};
char* arr11[] ={"","Eleven ","Twelve ","Thirteen ","Fourteen ","Fifteen ","Sixteen ","Seventeen "," Eighteen ","Nineteen "};
char* arr10[] = {"","Ten ","Twenty ","Thirty ","Forty ","Fifty ","Sixty ","Seventy ","Eighty ","Ninety "};
char* arr100[] = {"","Hundred ","Thousand ","Lacs ","Crore "};
int i;
int tmp;
if(num == 0)
{
strcat(str , "Zero");
return ;
} if(len > 3 && len % 2 == 0)
{
len++;
}
do
{
if(len > 3)
{
tmp = (num/(int)pow(10,len-2));
if(tmp/10 == 1 && tmp%10 != 0)
{
strcat(str ,arr11[tmp%10] ) ;
}
else
{
strcat(str , arr10[tmp/10]);
strcat(str , arr1[tmp%10]);
}
if(tmp)
strcat(str ,arr100[len/2]);
num = num % (int)(pow(10,len-2));
len -= 2;
}
else
{
tmp = num / 100;
if(tmp != 0)
{
strcat(str , arr1[tmp]);
strcat(str ,arr100[len/2]);
}
tmp = num % 100 ;
if(tmp/10 == 1 && tmp%10 != 0)
{
strcat(str ,arr11[tmp%10] ) ;
}
else
{
strcat(str , arr10[tmp/10]);
strcat(str , arr1[tmp%10]);
}
len = 0;
}
}while(len > 0);
}
use hashtabe for searching non unique element.if collision occures then print element.
- brijesh kumar jaiswal March 13, 2008