Problem to solve: given an array or sequence of numbers the goal is to find the Max number obtained by the multiplication of some 2 numbers in that sequence.
example of inputs and outputs
Input:
2
100000 90000
Correct output:
9000000000
Input:
3
1 2 3
Correct output:
6
My Solution: get the 2 Maximum numbers in the sequence given and multiply them my code works unless with one solution
My code
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void print(std::vector<int> const& input)
{
for (int i = 0; i < input.size(); i++) {
std::cout << input.at(i) << ' ';
}
}
int main()
{
vector<int> seq;
int n;
// Read the nb of elements in vect
cout << "please enter the number of elements in sequence"<<endl;
cin >> n;
// Read the vector
cout << "please enter the elements of the sequence"<<endl;
for (int i = 0; i < n; i++)
{
int input;
cin >> input;
seq.push_back(input);
}
cout << "sequence you entered" << endl;
print(seq);
// Find the 1st max element
double FisrtMax=*max_element(seq.begin(), seq.end());
cout <<endl<< "First Maximum Element is" << endl<< FisrtMax;
// remove the found element
std::vector<int>::iterator PosF = find(seq.begin(), seq.end(), FisrtMax);
seq.erase(PosF);
cout <<endl<< "sequence After removing the 1st maximum element" << endl;
print(seq);
// Find the 2nd max element
double SecMax = *max_element(seq.begin(), seq.end());
cout <<endl<< "Second Maximum Element is" << endl << SecMax;
//multiply the 2 elements
int total = (FisrtMax * SecMax);
cout <<endl<<"The Product of the 2 elemnts is "<< total;
return 0;
}
The Input :
please enter the number of elements in sequence
2
please enter the elements of the sequence
1000000
90000
The Output :
please enter the number of elements in sequence
2
please enter the elements of the sequence
1000000
90000
sequence you entered
10000002105376900002105376
First Maximum Element is
1e+06
sequence After removing the 1st maximum element
900002105376
Second Maximum Element is
90000
The Product of the 2 elements is -2147483648
There are several mistakes in the code:
cout << ... << ' '
syntax tries to print three whitespace characters letters using a single-quote which accepts a single letter not multiple. Use " "
instead.
The result produced can't be held by an integer, you need to define size_t
(which expands into unsigned long long
in most compilers).
Side Tips:
In this syntax:
for (int i = 0; i < input.size(); i++)
You're trying to compare an integer to size_t
(returned by size()
member function of vector class object). Rather than that, declare i
with size_t
.
In this syntax:
std::vector<int>::iterator PosF = find(seq.begin(), seq.end(), FisrtMax);
You don't need to define such long type std::vector<int>::iterator
, use auto
keyword here:
auto PosF = find(seq.begin(), seq.end(), FirstMax);
Code redefined:
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void print(std::vector<size_t> const &input) {
for (size_t i = 0; i < input.size(); i++)
std::cout << input.at(i) << " ";
}
int main(void) {
vector<size_t> seq;
int n;
// Read the nb of elements in vect
cout << "please enter the number of elements in sequence" << endl;
cin >> n;
// Read the vector
cout << "please enter the elements of the sequence" << endl;
for (int i = 0; i < n; i++) {
int input;
cin >> input;
seq.push_back(input);
}
cout << "sequence you entered" << endl;
print(seq);
// Find the 1st max element
double FisrtMax = *max_element(seq.begin(), seq.end());
cout << endl
<< "First Maximum Element is" << endl
<< FisrtMax;
// remove the found element
auto PosF = find(seq.begin(), seq.end(), FisrtMax);
seq.erase(PosF);
cout << endl << "sequence After removing the 1st maximum element" << endl;
print(seq);
// Find the 2nd max element
double SecMax = *max_element(seq.begin(), seq.end());
cout << endl
<< "Second Maximum Element is" << endl
<< SecMax;
//multiply the 2 elements
size_t total = (FisrtMax * SecMax);
cout << endl
<< "The Product of the 2 elements is " << total;
return 0;
}
Its Inputs:
please enter the number of elements in sequence
2
please enter the elements of the sequence
1000000
90000
It outputs:
sequence you entered
1000000 90000
First Maximum Element is
1e+06
sequence After removing the 1st maximum element
90000
Second Maximum Element is
90000
The Product of the 2 elemnts is 90000000000
Edit: This program successfully works on OnlineGDB and the program was compiled with C++14 flag.