-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLargestnSecondLargInt.cpp
30 lines (27 loc) · 1.07 KB
/
LargestnSecondLargInt.cpp
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
/*
Finding largest and secong largest ints in a array
with order of O(n)
Here we just keep to variables one,two to store largest and second largest respectively;
We do the usual method of comparing first for largest , then for second largest at the same time of checking the int.
But I did one mistake of not taking largest value into second large value , when another largest is found.Look down: */
#include <stdio.h>
#include <vector>
using namespace std;
int main(void) {
long long int i=0;
vector<long long int> arr;
char temp;
long long int one=0 , two=0,len=0,pos=0;
do{
scanf("%lld%c", &i, &temp);
arr.push_back(i);
len++;
if(i>one)
{two=one;one=i;pos=len;} //two=one is IMPORTANT, as we are transfering the then largest to become second largest;
else if(i>two && len!=pos)//else we will miss that large value
two=i; //then now largest(i) becomes one/largest;pos stores new largest position/index;
} while(temp!= '\n');
for(i=0;i<arr.size();i++)
printf("%d ",arr[i]);
return 0;
}