-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCODECHEF_SMPAIR.c
70 lines (70 loc) · 972 Bytes
/
CODECHEF_SMPAIR.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//CODECHEF Problem : The Smallest Pair.(Beginner)
//Problem Code : SMPAIR
//Author : By PIYUSH.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
}*top=NULL;
void push(int);
void disp(int);
int sum(void);
int main()
{
int t,no,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&no);
n=no;
push(no);
disp(n);
printf("%d",sum());
printf("\n");
}
return 0;
}
void push(int no)
{
int n;
while(no--)
{
struct node *tmp;
tmp = (struct node *)malloc(sizeof(struct node));
scanf("%d",&n);
tmp->info=n;
tmp->link=top;
top=tmp;
}
}
void disp(int no)
{
int i,temp;
struct node *tmp;
for(i=1;i<no;i++)
{
tmp=top;
while(tmp!=NULL)
{
if(tmp->info > tmp->link->info)
{
temp=tmp->info;
tmp->info = tmp->link->info;
tmp->link->info=temp;
}
tmp=tmp->link;
}
}
}
int sum(void)
{
struct node *tmp;
int sum=0;
tmp=top;
sum=sum + tmp->info;
tmp=tmp->link;
sum=sum + tmp->info;
return sum;
}