-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlargesmallsum.cpp
More file actions
64 lines (60 loc) · 1.47 KB
/
largesmallsum.cpp
File metadata and controls
64 lines (60 loc) · 1.47 KB
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
#include<iostream>
using namespace std;
int largesmallsum(int size,int arr[])
{
int sum=0,lr1=0,large=0,sm1=INT_MAX,small=INT_MAX,temp;
//sort in decreasing order of even places number then 2 indexed number will be 2nd largest
for(int i = 0; i < size ; i+=2)
{
for(int v=i+2;v<size;v+=2)
{
if(arr[i]<arr[v])
{
temp = arr[i];
arr[i]=arr[v];
arr[v]=temp;
}
}
}
//sort in increasing order of odd places number then 3 indexed number will be 2nd smallest
for(int j = 1; j < size ; j+=2)
{
for(int u=j+2;u<size;u+=2)
{
if(arr[j]>arr[u])
{
temp = arr[j];
arr[j]=arr[u];
arr[u]=temp;
}
}
}
for(int i = 0; i < size ; i++)
{
cout<<arr[i]<<endl;
}
sum = arr[2] + arr[3];
return sum;
}
int main()
{
int size;
cout<<"Enter the size of array: ";
cin>>size;
int arr[size];
if(size<=3 || size == 0)
cout<<"0";
else{
for(int i = 0; i < size ; i++)
{
cout<<"Enter the elements: ";
cin>>arr[i];
}
for(int i = 0; i < size ; i++)
{
cout<<arr[i]<<endl<<endl<<endl;
}
cout<<"Answer is: "<<largesmallsum(size, arr);
}
return 0;
}