pintuguptajuit(PINTU GUPTA)
BAN USER- 0of 0 votes
Answerswap to find the longest arithmetic sequence in given array... return number of element in seriese...
- pintuguptajuit(PINTU GUPTA) in India
simple test case..
sizeofarray=4
array element
3 4 5 8
output
3
sample test case
size of array =10
array element..
-1 1 3 7 11 15 19 20 21 22
output
5| Report Duplicate | Flag | PURGE
Amazon Intern Algorithm - 0of 0 votes
Answerswaf for a linked listed ..
- pintuguptajuit(PINTU GUPTA) in India
shift all digit to first then consonant then vowel ..
such that list contain only one time one digit , consonant , vowel ..
List *(List *head);
input :
2->a->5->a->2->b->o->n->5->n.
output..
2->5->b->n->a->o..| Report Duplicate | Flag | PURGE
Amazon Intern Algorithm - 0of 0 votes
Answerswap to take one rotate a square matrix anticlock wise by 90 degree and add a particuler number after rotation to each prime column.
- pintuguptajuit(PINTU GUPTA) in India
function prototype should be..
void rotate(int a[][],int size, int keytobeadded);...
sample test case....
input
size=3
keytobeadded=5;
square matrix :
1 2 3
4 5 6
7 8 9
output should be
3 11 9
2 10 8
1 9 7...........| Report Duplicate | Flag | PURGE
Amazon Intern Algorithm - -3of 3 votes
Answerswrite a function that print TRUE if (){}[] are balanced in expression .. otherwise return FALSE.
- pintuguptajuit(PINTU GUPTA) in India
[{()}] currect.. priority of [ >}>)..........it should be preserved
....................................................................................
void check(char *a)
{
struct s
{
char ch;
s*next;
};
s *st=NULL;
s*node=NULL;
if(a=='\0')
{
cout<<"TRUE";
return ;
}
else
{
while(a[0]!='\0')
{
{
node=(s*)malloc(sizeof(s));
char c=a[0];
switch(c)
{
case '[':
if(st==NULL)
{
node->ch=c;
node->next=NULL;
st=node;
a++;
}
else
{
if(st->ch=='('||st->ch=='{')
{
cout<<"FALSE";
return ;
}
else
{
node->ch=c;
node->next=st;
st=node;
a++;
}
}
break;
case '{':
if(st==NULL)
{
node->ch=c;
node->next=NULL;
st=node;
a++;
}
else
{
if(st->ch=='(')
{
cout<<"FALSE";
return ;
}
else
{
node->ch=c;
node->next=st;
st=node;
a++;
}
}
break;
case '(':
if(st==NULL)
{
node->ch=c;
node->next=NULL;
st=node;
a++;
}
else
{
node->ch=c;
node->next=st;
st=node;
a++;
}
break;
case ')':
if(st==NULL||st->ch=='{'||st->ch=='[')
{
cout<<"FALSE";
return ;
}
else
{
if(st->ch=='(')
{
s *newnode=st;
st=st->next;
free(newnode);
a++;
}
}
break;
case '}':
if(st==NULL||st->ch=='('||st->ch=='[')
{
cout<<"FALSE";
return ;
}
else
{
if(st->ch=='{')
{
s *newnode=st;
st=st->next;
free(newnode);
a++;
}
}
break;
case ']':
if(st==NULL||st->ch=='{'||st->ch=='(')
{
cout<<"FALSE";
return ;
}
else
{
if(st->ch=='[')
{
s *newnode=st;
st=st->next;
free(newnode);
a++;
}
}
break;
default :
a++;
if(a[0]=='\0')
{
cout<<"TRUE";
return ;
}
break;
}
}
}
if(st==NULL&&a[0]=='\0')
{
cout<<"TRUE";
return ;
}
if(st!=NULL)
{
cout<<"\nFALSE";
return ;
}
}
}| Report Duplicate | Flag | PURGE
Amazon Intern
1 reverse the string O(n) time
2. then reverse each word,..
u can apply divide and conquore methde then apply same method to each word..
time complexity O(n).,
struct node
{
int data;
node *next;
};
void swapfirst_and_last_kth_node(node **head,int k)
{
node *p1=*head,*p2=*head;
while(p1!=NULL && k>1)
{
(p1)=(p1)->next;
k--;
}
if(k!=0 && (p1)==NULL)
{
cout<<"\n Error in the code";
return ;
}
node *p3=p1;
while((p1->next))
{
(p2)=(p2)->next;
(p1)=(p1)->next;
}
int tm=(p3)->data;
(p3)->data=(p2)->data;
(p2)->data=tm;
}
#include<iostream>
using namespace std;
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<math.h>
void uniquechar(char *str)
{
if(!str)
return ;
else
{
int *ta=new int[256];
for(int i=0;i<256;i++)
ta[i]=0;
//for(int i=0;str[i];i++)
//tstr[str[i]]++;
int max=-1;
int j,k;
int si=0,li=0,count,f=0;
for(int i=0;i<str[i]!='\0';i++)
{
k=f;
j=i;
count=i-f;
while(str[i]!='\0'&&ta[str[i]]==0)
{
ta[str[i]]=1;
count++;
j++;
i++;
}
if(str[i]=='\0'||str[i]!='\0')
{
if(max<=count)
{
max=count;
si=k;li=j-1;
}
if(str[i]=='\0')
break;
}
if(str[i]=='\0')
break;
for(int m=si;m<=li;m++)
{
if(str[m]!=str[i])
ta[str[m]]=0;
else
{f=m+1;
break;
}
}
}
cout<<"\nThe from index "<<si<<" to "<<li<<" of length "<<li-si+1<<" is the max unique character ::\n";
for(int i=si;i<=li;i++)
{
cout<<str[i];
}
}
}
int main()
{
char *str=new char[100];
cin>>str;
uniquechar(str);
getch();
return 0;
}
order O(n) complexity..............
#include<iostream>
using namespace std;
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<math.h>
void uniquechar(char *str)
{
if(!str)
return ;
else
{
int *ta=new int[256];
for(int i=0;i<256;i++)
ta[i]=0;
//for(int i=0;str[i];i++)
//tstr[str[i]]++;
int max=-1;
int j,k;
int si=0,li=0,count,f=0;
for(int i=0;i<str[i]!='\0';i++)
{
k=f;
j=i;
count=i-f;
while(str[i]!='\0'&&ta[str[i]]==0)
{
ta[str[i]]=1;
count++;
j++;
i++;
}
if(str[i]=='\0'||str[i]!='\0')
{
if(max<=count)
{
max=count;
si=k;li=j-1;
}
if(str[i]=='\0')
break;
}
if(str[i]=='\0')
break;
for(int m=si;m<=li;m++)
{
if(str[m]!=str[i])
ta[str[m]]=0;
else
{f=m+1;
break;
}
}
}
cout<<"\nThe from index "<<si<<" to "<<li<<" of length "<<li-si+1<<" is the max unique character ::\n";
for(int i=si;i<=li;i++)
{
cout<<str[i];
}
}
}
int main()
{
char *str=new char[100];
cin>>str;
uniquechar(str);
getch();
return 0;
}
order O(n) complexity..............
it should be 36
- pintuguptajuit(PINTU GUPTA) March 24, 2013linked list contain...
a->m->a->z->o->n
output
a->a->o->m->z->n
because of vowel and consonent should be preserved....
its time complexity will be O(n) in worst case..
- pintuguptajuit(PINTU GUPTA) March 24, 2013#include<iostream>
using namespace std;
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<math.h>
void input(int *a[3],int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>a[i][j];
}
}
}
void print(int *a[3],int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<" "<<a[i][j];
}
cout<<"\n";
}
}
void rotate(int *a[3],int n,int k)
{
int temp;
int nc=n;
for(int i=0;i<n/2;i++)
{
if((nc!=2))
for(int j=i;j<=n-2;j++)
{
temp=a[i][j];
a[i][j]=a[j][n-i-1];
a[j][n-i-1]=a[n-i-1][n-j-1];
a[n-i-1][n-j-1]=a[n-j-1][i];
a[n-j-1][i]=temp;
}
if((nc==2))
for(int j=i;j<n/2;j++)
{
temp=a[i][j];
a[i][j]=a[j][n-i-1];
a[j][n-i-1]=a[n-i-1][n-j-1];
a[n-i-1][n-j-1]=a[n-j-1][i];
a[n-j-1][i]=temp;
}
nc-=2;
}
print(a,n);
for(int i=2;i<n;i++)
{
int f=0;
if(i==2)
{
for(int j=0;j<n;j++)
{
a[j][i-1]+=k;
}
}
else
{
for(int m=2;i<sqrt(i);m++)
{
if(i%2)
{
f=1;
break;
}
}
if(!f)
{
for(int j=0;j<n;j++)
{
a[j][i-1]+=k;
}
}
}
}
print(a,n);
}
int main()
{
int n;
cin>>n;
int **a=(int **)malloc(sizeof(int *)*n);
for(int i=0;i<n;i++)
{
a[i]=new int[n];
}
input(a,n);
cout<<"\n";
rotate(a,n,2);
getch();
return 0;
}
#include<iostream>
using namespace std;
#include<stdlib.h>
#include<conio.h>
#include<string.h>
struct node
{
char data;
node *next;
};
node *GetVowelFirstNumber(node *head)
{
if(!head)
return head;
else
{
node *list1,*list11;
node *list2,*list22;
node *list3,*list33;
list1=list11=list22=list2=list33=list3=NULL;
char c;
char ca[256];
for(int i=0;i<256;i++)
ca[i]=0;
while(head)
{
c=head->data;
if(c=='a'||c=='A'||c=='e'||c=='E'||c=='i'||c=='I'||c=='u'||c=='U'||c=='o'||c=='O')
{
if(ca[c]==0)
{
// cout<<"\n vowel";
ca[c]++;
if(!list1)
{
list1=list11=head;
head=head->next;
list11->next=NULL;
}
else
{
list11->next=head;
head=head->next;
list11=list11->next;
list11->next=NULL;
}
}
else
{
head=head->next;
}
}
else
{
if((c)>=48&&(c)<=57)
{
// cout<<"\n NUmber\n";
if(ca[c]==0)
{
ca[c]++;
if(!list3)
{
list3=list33=head;
head=head->next;
list33->next=NULL;
}
else
{
list33->next=head;
head=head->next;
list33=list33->next;
list33->next=NULL;
}
}
else
{
head=head->next;
}
}
else
{
if(ca[c]==0)
{
cout<<"\n consonant";
ca[c]++;
if(list2==NULL)
{ cout<<"list2\n";
list2=list22=head;
head=head->next;
list22->next=NULL;
}
else
{
cout<<"list2\n";
list22->next=head;
head=head->next;
list22=list22->next;
list22->next=NULL;
}
}
else
{
head=head->next;
}
}
}
}
// list3 number
// list2 cons
// list1 vowel
if(list1)
{
if(list2)
{
if(list3)
{
list33->next=list2;
list22->next=list1;
head=list3;
}
else
{
list22->next=list1;
head=list2;
//cout<<"\n"<<head->data<<"\n";
//return head;
}
}
else
{
if(list3)
{
list33->next=list1;
head=list3;
}
else
{
head=list1;
}
}
}
else
{
if(list2)
{
if(list3)
{
list33->next=list2;
head=list3;
}
else
{
head=list2;
}
}
else
{
head=list2;
}
}
return head;
}
}
int main()
{
char *a=new char[100];
cin>>a;
node *n=NULL;
for(int i=0;a[i]!='\0';i++)
{
inseart(&n,a[i]);
}
n=GetVowelFirstNumber(n);
display(n);
getch();
return 0;
}
#include<iostream>
using namespace std;
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<math.h>
int glas(int *a,int n)
{
int i,j,k;
if(n==1||n==2)
{
return n;
}
else
{
i=j=k=0;
int d=a[1]-a[0];
int max=-1;
int count=2;
for(int i=1;i<n;)
{
count=2;
while((i+1<n)&&a[i+1]-a[i]==d)
{
count++;
i++;
}
if(max<count)
{
max=count;
}
d=a[i+1]-a[i];
i++;
}
return max;
}
}
int main()
{
int n;
cin>>n;
int *a=new int[n];
for(int i=0;i<n;i++)
cin>>a[i];
cout<<glas(a,n);
getch();
return 0;
}
long fiboo(long int n )
{
long int pre1=0,pre2=1;
if(n==1)
return 0;
if(n==1)
return 1;
for(long int i=2;i<=n;i++)
{
long int temp=pre2
pre2=pre2+pre1;
pre1=temp;
}
return pre2;
}
//// parenthesis cheaking........
void check(char *a)
{
struct s
{
char ch;
s*next;
};
s *st=NULL;
s*node=NULL;
if(a=='\0')
{
cout<<"TRUE";
return ;
}
else
{
while(a[0]!='\0')
{
{
node=(s*)malloc(sizeof(s));
char c=a[0];
switch(c)
{
case '[':
if(st==NULL)
{
node->ch=c;
node->next=NULL;
st=node;
a++;
}
else
{
if(st->ch=='('||st->ch=='{')
{
cout<<"FALSE";
return ;
}
else
{
node->ch=c;
node->next=st;
st=node;
a++;
}
}
break;
case '{':
if(st==NULL)
{
node->ch=c;
node->next=NULL;
st=node;
a++;
}
else
{
if(st->ch=='(')
{
cout<<"FALSE";
return ;
}
else
{
node->ch=c;
node->next=st;
st=node;
a++;
}
}
break;
case '(':
if(st==NULL)
{
node->ch=c;
node->next=NULL;
st=node;
a++;
}
else
{
node->ch=c;
node->next=st;
st=node;
a++;
}
break;
case ')':
if(st==NULL||st->ch=='{'||st->ch=='[')
{
cout<<"FALSE";
return ;
}
else
{
if(st->ch=='(')
{
s *newnode=st;
st=st->next;
free(newnode);
a++;
}
}
break;
case '}':
if(st==NULL||st->ch=='('||st->ch=='[')
{
cout<<"FALSE";
return ;
}
else
{
if(st->ch=='{')
{
s *newnode=st;
st=st->next;
free(newnode);
a++;
}
}
break;
case ']':
if(st==NULL||st->ch=='{'||st->ch=='(')
{
cout<<"FALSE";
return ;
}
else
{
if(st->ch=='[')
{
s *newnode=st;
st=st->next;
free(newnode);
a++;
}
}
break;
default :
a++;
if(a[0]=='\0')
{
cout<<"TRUE";
return ;
}
break;
}
}
}
if(st==NULL&&a[0]=='\0')
{
cout<<"TRUE";
return ;
}
if(st!=NULL)
{
cout<<"\nFALSE";
return ;
}
}
}
struct *node
{
char val;
node *next;
};
node *GetVowelFirst(node *head)
{
if(!head)
return head;
else
{
node *list1,*list11;
node *list2,*list22;
list1=list11=list22=list2=NULL;
char c;
node *dhead=head;
while(head)
{
c=head->data;
if(c=='a'||c=='A'||c=='e'||c=='E'||c=='i'||c=='I'||c=='u'||c=='U'||c=='o'||c=='O')
{
if(!list1)
{
list1=list11=head;
head=head->next;
list11->next=NULL;
}
else
{
list11->next=head;
head=head->next;
list11=list11->next;
list11->next=NULL;
}
}
else
{
if(!list2)
{
list2=list22=head;
head=head->next;
list22->next=NULL;
}
else
{
list22->next=head;
head=head->next;
list22=list22->next;
list22->next=NULL;
}
}
}
list11->next=list2;
head=list1;
return head;
}
}
1. traverse list .......... one by by........ breack one node at time
- pintuguptajuit(PINTU GUPTA) March 25, 20132 use four pointer .......
3 create new sorted list.....
4 inseart each node at exact position in newly created list.......
worst case time O(n^2) if n number of node ........