raihanruhin
BAN USERfull code here:
#include<iostream>
using namespace std;
struct llist{
int x;
llist *next;
llist()
{
next=NULL;
}
};
void swp(llist *head)
{
llist *a,*b;
if(head->next==NULL || head->next->next==NULL) return;
a=head;
b=head->next;
int tmp=a->x;
a->x=b->x;
b->x=tmp;
if(b->next!=NULL)
swp(b->next);
return;
}
main()
{
llist *lst, *tr;
lst=new llist;
tr=lst;
cout<<"Enter number of nodes: ";
int n,x;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>x;
lst->x=x;
lst->next=new llist();
lst=lst->next;
}
swp(tr);
cout<<endl;
while(tr->next!=NULL)
{
cout<<tr->x<<" ";
tr=tr->next;
}
}
input-output:
Enter number of nodes: 5
2 5 9 6 7
5 2 6 9 7
void swp(llist *head)
{
llist *a,*b;
if(head->next==NULL || head->next->next==NULL) return;
a=head;
b=head->next;
int tmp=a->x;
a->x=b->x;
b->x=tmp;
if(b->next!=NULL)
swp(b->next);
return;
}
mn[0]=1e9;
mx[11]=maxx=0;
for(int i=1;i<=10;i++) cin>>arr[i];
for(int i=1;i<=10;i++) mn[i]=min(mn[i-1], arr[i]);
for(int i=10;i>=1;i--) mx[i]=max(mx[i+1], arr[i]);
for(int i=1;i<=10;i++) maxx=max(maxx, mx[i] - mn[i]);
cout<<maxx<<endl;
this is the code, is their any better approach?
- raihanruhin July 23, 2013first sort the array then check linearly.
complexity nlogn.
use string.
- raihanruhin May 08, 2013
arranging the array in ascending order is not in linear time. sorting is O(nlogn).
- raihanruhin August 04, 2013