Given a list of N + 1 numbers, N mixed up with them, find the maximum number between this N numbers.
EXPLANATION
Let A be the original list of size N. The number N was added in this list. Let the updated list be B. We can find the list A from the list B by finding any occurrence of the number N and deleting it. Finding maximum in a list can be done by iterating over its elements in linear time.
Parsing the input into a list of integers
For reading a line from standard input, you can use
getline(cin, s)
, it will read a line and save it in string s.
You can parse the input string into integers as follows. Use [string stream][4], read a line by
getline
and put it into some stringstream, then read ints one by one until it has some. You can check if it has some using
ss >> a
, it will return false if nothing is remained in stringstream (see my code for better understanding).
**IMPLEMENTATION** -
Setter's code - [here][1] (using getline and stringstream).
Tester's code - [here][2] (using getline and parsing the input manually).
Second tester's code - [here][3] (using getline and parsing the input manually).
[1]: http://www.codechef.com/download/Solutions/LTIME50/Setter/LOSTMAX.cpp
[2]: http://www.codechef.com/download/Solutions/LTIME50/Tester1/LOSTMAX.cpp
[3]: http://www.codechef.com/download/Solutions/LTIME50/Tester2/LOSTMAX.cpp
[4]: http://www.cplusplus.com/reference/sstream/stringstream/
I submitted the first problem yesterday and got WA. I got demotivated and left because the first problem was the easiest. Today, I came back online and saw (with some pleasant surprise and happiness) that it did get accepted ! Can someone explain what happened ?
What is wrong with my solution? I have tested all possible test cases.This is my submission for LOSTMAX from LTIME50. Further clarifications on where I went wrong is appreciated. Thank You.
@sprea27
If c[0]=c.size()-1 and it is the greatest element then your code give that as output which is wrong.
Input
3 1 2 1
your code output:3
correct output: 2
So put c=0 or any negative number and run loop from 0.You will get AC.
Second TC has array numbers >9, i.e. 2 digit numbers. If you are taking entire line as input (as string), then make sure you are correctly assigning values to array. I.e. storing 19 in array instead of 1 and 9 in 2 adjacent indices.
Maybe languages like python can help a Lot in these cases.
We can take input without reading the number of inputs in single line in python
while input().split() and using map().
Makes code really simple.
Clean Code https://www.codechef.com/viewsolution/24351745