raghuram


{ City } bangalore
< Country > india
* Profession *
User No # 6167
Total Questions Posted # 0
Total Answers Posted # 16

Total Answers Posted for My Questions # 0
Total Views for My Questions # 0

Users Marked my Answers as Correct # 287
Users Marked my Answers as Wrong # 161
Questions / { raghuram }
Questions Answers Category Views Company eMail




Answers / { raghuram }

Question { Google, 45522 }

Given an array of size N in which every number is between 1
and N, determine if there are any duplicates in it. You are
allowed to destroy the array if you like.


Answer

guys..sort the array using quicksort..and check adjacent
elements..u can know there are duplicates or
not..complexity-O(nlogn)+n-1

Is This Answer Correct ?    22 Yes 10 No

Question { Wipro, 9915 }

Given an array of characters which form a sentence of words,
give an efficient algorithm to reverse the order of the words
(not characters) in it.


Answer

/*in 2n comparisons*/


#include
#include
#include

int count=0;
void rev_word(char str[20],int m,int n)
{
int i,l,k;
k=n-m+1;
if(k%2==0)
l=(k/2-1);
else
l=k/2;
k=n;
for(i=m;i<=m+l;i++)
{
char t=str[i];
str[i]=str[k];
str[k]=t;
k--;
}
}
int main()
{
char str[100];
int i,j=0;
cout<<"\n\nenter string:";
gets(str);

rev_word(str,0,strlen(str)-1);


for(i=0;i<=strlen(str);i++)
{
if(str[i]==' '||str[i]=='\0')
{
rev_word(str,j,i-1);
j=i;
while(str[j]==' ')
j++;
i=j;
}
}
cout<<"\n\nsentence with order of words reversed is:";
cout< return 0;
}



Is This Answer Correct ?    2 Yes 0 No


Question { Microsoft, 28084 }

Give a very good method to count the number of ones in a 32
bit number.
(caution: looping through testing each bit is not a solution)


Answer

#include
#include
/*no. of 1's in no. of 1's steps*/
int count(unsigned long int n)
{
int count=0;
while(n)
{
count++;
n=n&n-1;
}
return count ;
}


Is This Answer Correct ?    41 Yes 26 No

Question { Microsoft, 56774 }

program to Reverse a linked list


Answer

node *reverse(node *first)


{
node *cur,*temp;


cur=NULL;

while(first!=NULL)


{temp=first;

first=first->link;

temp->link=cur;

cur=temp;
}

return cur;

}

Is This Answer Correct ?    137 Yes 52 No

Question { Microsoft, 13394 }

write a program to Insert in a sorted list


Answer

node *insert(node *first,int data)
{node *temp,*trav,*cur;
temp->info=data;
temp->next=NULL;
trav=first;
if(datainfo)
{temp->next=first;
first=temp;
return first;
}
while(data>trav->info&&trav->next!=NULL)
{cur=trav;
trav=trav->next;
}
if(data>trav->info&&trav->next==NULL)
trav->next=temp;
else
{cur->next=temp;
temp->next=trav;
}
return first;
}

Is This Answer Correct ?    10 Yes 5 No

Question { Microsoft, 16449 }

Write a routine that prints out a 2-D array in spiral order


Answer

#include
#include
#include
main()
{
int n,m,i=0,j=0,k=0,l=0,p=0,q=1,r=0,s=1,a=40,b=25,ar[100];
clrscr();
cout<<"enter n:";
cin>>n;
printf("enter array elements:");
for(m=0;m scanf("%d",&ar[m]);
gotoxy(a,b);
m=0;
printf("%d",ar[m++]);
while(m {
while(i<=j)
{
i++;
a+=4;
gotoxy(a,b);
printf("%d",ar[m++]);
if(m>=n)
goto next;
}

while(k<=l)
{ k++;
b+=4;
gotoxy(a,b);
printf("%d",ar[m++]);
if(m>=n)
goto next;
}
while(p<=q)
{
p++;
a-=4;
gotoxy(a,b);
printf("%d",ar[m++]);
if(m>=n)
goto next;

}
while(r<=s)
{
r++;
b-=4;
gotoxy(a,b);
printf("%d",ar[m++]);
if(m>=n)
goto next;
}
j+=2;
l+=2;
q+=2;
s+=2;
i=0;
k=0;
p=0;
r=0;

}
next:getch();
return 0;
}




Is This Answer Correct ?    1 Yes 13 No

Question { 19570 }

Print an integer using only putchar. Try doing it without
using extra storage.


Answer

void putlong(unsigned long x)
{ if (x > 10) putlong(x / 10);
putchar(x % 10+'0');
}
main()
{
long int a;
printf("enter long integer:");
scanf("%d",&a);
putlong(a);
return 0;

}

Is This Answer Correct ?    22 Yes 5 No

Question { Microsoft, 20940 }

Write out a function that prints out all the permutations of
a string.

For example, abc would give you abc, acb, bac, bca, cab,
cba. You can assume that all the characters will be unique.


Answer

/*guys..I have implemented Jhonson trotter algorithm..u can
print permutations of 123..n. implement the same for strings!*/


#include
#include
int min(int a[10],int n)
{
int i,m=1;
for(i=2;i<=n;i++)
{
if(a[m]>a[i])
m=i;
}
return m;
}

void swap(int &a,int &b)
{
int t;
t=a;
a=b;
b=t;
}
int main()
{
int i,j,k,n,flag=0,l,m;
int d[100],a[100];
clrscr();
cout<<"\n\nenter n:\n\n";
cin>>n;
for(i=1;i<=n;i++)
a[i]=i;
for(i=1;i<=n;i++)
d[i]=i-1;
cout<<"\n\npermutations generated for integer 12...n
are:\n\n";
for(i=1;i<=n;i++) //display the given no.
cout< cout<<"\t";
do
{
flag=0;
k=min(a,n);
for(i=1;i<=n;i++)
{
if((a[i]>a[k])&&(d[i]!=0)&&(a[d[i]] mobile integer.
k=i;
}
if(a[k]==1)
break;
l=d[k]; //copy of direction of mobile integer.
m=a[k]; //copy of mobile integer.


if(d[k]==k+1&&d[k+1]==k) //swap directions.
{

if(d[k]==n)
{ d[k+1]=0;
d[k]=k-1;
}

else if(d[k+1]==1)
{
d[k]=0;
d[k+1]=k+2;
}
else
{ d[k]=k-1;
d[k+1]=k+2;
}
}
else if(d[k]==k-1&&d[k-1]==k)
{
d[k]=k+1;
d[k-1]=k-2;
}
/*cout< cout<<"\t";
if u want to know directions of integers.
*/
swap(a[k],a[l]); //swap mobile integer and integer it is
pointing to.
for(i=1;i<=n;i++)
cout< cout<<"\t";
for(i=1;i<=n;i++) //reverse directions of integers
greater than
//mobile integer.
{
if(a[i]>m)
{
if(d[i]==0&&i==n)
d[i]=i-1;
else if(d[i] d[i]=i+1;
else if(d[i]>i)
d[i]=i-1;
}
}
for(i=1;i {
if(a[i] {

if(d[i+1]!=0)
flag=1;
}

else if(a[i]>a[i+1])
{

if(d[i]!=0)
flag=1;
}
}
}
while(flag==1); //if no mobile
integer(flag=0)terminate the program.
getch();
return 0;
}

Is This Answer Correct ?    5 Yes 5 No

Question { 36181 }

Program to find the largest sum of contiguous integers in
the array. O(n)


Answer

#include
#include
main()
{
int s=-1,m=1,n,max=0,e=-1,sum=0,i,a[20];
cout<<"enter n:";
cin>>n;
cout<<"enter elements:";
for(i=0;i cin>>a[i];
for(i=0;i {
if(m==1)
{
if(a[i]>0)
s=i;
m=0;
}
if(a[i]+sum>0)
sum+=a[i];
else
{

m=1;
sum=0;
}
if(sum>max)
{
max=sum;
e=i;
}
}
cout<<"max sum is:"< cout<<"\n\nmax sum producing sub array is:";
if(s!=-1&&e!=-1)
for(i=s;i<=e;i++)
cout< getch();
return 0;
}

Is This Answer Correct ?    11 Yes 13 No

Question { 36181 }

Program to find the largest sum of contiguous integers in
the array. O(n)


Answer

#include
#include
/*prints subarray producing maximum sum..Efficiency is O(n)*/
main()
{
int flag=0,n,i,c=0,sum=0,max=0,a[25],s=-1,e=-1;
cout<<"enter n:";
cin>>n;
cout<<"enter elements:";
for(i=1;i<=n;i++)
cin>>a[i];
for(i=1;i<=n;i++)
{
if(a[i]+sum>0)
sum+=a[i];
else
{
sum=0;
if(c!=0)
c=1;
}
if(sum>max)
{
max=sum;
e=i;
s=e-c;
c++;
}
}
cout<<"max sum is:"< cout<<"\n\nmax sum producing sub array is:";
if(s!=-1&&e!=-1)
for(i=s;i<=e;i++)
cout<<"\t"< getch();
return 0;
}

Is This Answer Correct ?    11 Yes 9 No

Question { Accenture, 17049 }

Find your day from your DOB?


Answer

No..it's giving Tuesday only..U enter 16 4 1985 n press enter.
It will show Tuesday only..Logic is correct.I made a mistake
by telling to enter date in dd/mm/yyyy format..but u just
enter date in dd mm yyyy format(space between dd,mm and
yyyy)..u will get correct ans.

Raghuram.A

Is This Answer Correct ?    1 Yes 0 No

Question { Accenture, 17049 }

Find your day from your DOB?


Answer



#include
#include
int leap(int y)
{
if(y%4==0&&y%100!=0||y%400==0)
return 1;
else
return 0;
}
main()
{
int d,m,y,i,rem;
int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
long int days=0;
clrscr();
cout<<"enter date in dd/mm/yyyy format:";
cin>>d>>m>>y;
for(i=1;i if(leap(i))
days+=366;
else
days+=365;
for(i=1;i if(leap(y)&&i==2)
days+=29;
else
days+=a[i-1];
days+=d;
rem=days%7;
switch(rem)
{case 1:cout<<"monday";
break;
case 2:cout<<"tuesday";
break;
case 3:cout<<"wedday";
break;
case 4:cout<<"thursday";
break;
case 5:cout<<"friday";
break;
case 6:cout<<"saturday";
break;
case 0:cout<<"sunday";

}
getch();
return 0;
}

Is This Answer Correct ?    2 Yes 0 No

Question { Symantec, 21241 }

String reverse with time complexity of n/2 with out using
temporary variable.


Answer

#include

#include//complexity-n/2

int main()
{
int i,l,l1;
char str[100];
cout<<"enter string:";
gets(str);
l=strlen(str);
if(l%2==0)
l1=(l/2-1);
else
l1=l/2;
for(i=0;i<=l1;i++)/*swap elements from 2 ends till u reach
middle part of array*/
{
char t=str[i];
str[i]=str[l-i-1];
str[l-i-1]=t;
}
str[l]=0;
cout<<"\n\nreversed string is:"< getch();
return 0;
}

Is This Answer Correct ?    2 Yes 11 No

Question { Google, 15625 }

write the function. if all the character in string B appear in
string A, return true, otherwise return false.


Answer

#include
#include
#include
int main()
{
char str1[100],str2[100];
int flag1=0,flag=0;
cout<<"\n\nenter 1st string:";
cin>>str1;
cout<<"\n\nenter 2nd string:";
cin>>str2;
int i=0,j=0;
for(i=0;i { flag=0;
for(j=0;j {
if(str1[i]==str2[j])
{flag=1;
str2[j]=' ';
break;
}
}
if(flag==0)
{
flag1=1;
cout<<"\n\nnot anagrams";
break;
}
}
if(flag1!=1)
cout<<"\nanagrams";


return 0;

}



Is This Answer Correct ?    1 Yes 0 No

Question { 10343 }

How do we swap or interchange any 2 numbers without using
Temporary variable...Anybody can pls answer it.. Thanks in
Advance


Answer

int x,y;

x =x-y;
y =y+x;
x =y-x;

or

int x,y;

x=x^y;
y=x^y;
x=x^y;

Is This Answer Correct ?    11 Yes 7 No

 [1]   2    Next