Click here to Skip to main content
15,920,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
(I posted this on CodeReview as well, I hope it's ok. I would love to get different perspectives & suggestion across different platforms).
I am following a question on a programming challenge website (here's the original reference). What I have is a house (start & endpoint) and some types of trees at their respective location points. Each of these trees produce some fruits that would fall at different distances. My job is to find if the fruits have fallen on the house area (inside of start & endpoint). Illustration is here.

Instead of `cin` everything into a single var (according to the illustration), I want to make my code more dynamic, therefore I decided to use vector structure following a suggestion by @CPallini on my prev question.
house     = location point of house (start & endpoint)
trees     = location point of each tree
fruits    = number of fruits fall
fruit     = distance of each fallen fruit
counting  = number of fruits fall inside the house area

Constraints :
1 <= house, trees, fruits, distance <= 10^5
-10^5 <= distance <= 10^5
a < s < t < b (following the picture)

Input Format :
Sample Input : (sample test case) 
7 11      // house location
5 15      // tree location
3 2       // how many fruits fall from each tree
-2 2 1    // distance of 3 fruits fell from tree A
5 -6      // distance of 2 fruits fell from tree A
    
Sample Output :
1  // tree loc = 5 , fruits loc : -2 2 1. So, (5-2=3) (5+2=7 - inside house!) (5+1 =6)
1  // tree loc = 15, fruits loc : 5 -6.   So, (15+5=20) (15-6=9 - inside house!) 


What I have tried:

My code is a working code, it works on a several test cases (as in sample input), but failed some cases (sample inputs) as well, including this and this. Any improvement I can do with my code or advice on better approach ? Here's my code :
C++
#include <iostream>
#include <sstream>
#include <vector>
#include <string>

using std::cout;
using std::cin;
using std::getline;
using std::cerr;
using std::exit;
using std::vector;
using std::string;
using std::stringstream;
using std::istringstream;

vector<int> house;		        // house location (start & endpoint)
vector<int> trees;		        // location point of each trees
vector<int> fruits;		        // number of fruits fall
vector<vector <int>> fruit_loc; // each fallen fruit location
vector<int> fruit_onsite;	    // number of fruits fall inside house area

istringstream iss;
const double max_value = 100000;
const double min_value = -100000;

void reset(){
	string line;
	int data  = 0; 
	iss.clear(); 
	iss.seekg(0); 
	line.clear();
}

void input(vector<int>& input){ //Input function will be for house & fruits                                                        
	string line;
	int data  = 0; 
	
	getline(cin, line);
	iss.str(line);  
	while (iss >> data){
		// constraints : 1 <= trees <= 10^5 
		if ((data >= 1) && (data <=  max_value)){                                                      
			input.push_back(data);
		} else { 
			cerr << "input is less than 1 or bigger than 10^5"; 
			exit(1); }                   
	}
}

void input_tree(vector<int>& input,vector<int>& house){                                                         
	string line;
	int data  = 0; 
	
	getline(cin, line);
	iss.str(line);  
	while (iss >> data){
		// constraints : 1 <= trees <= 10^5 & tree can't be inside house area (a <s <t <b)
		if (((data < house[0]) || (data >  house[1])) && ((data >= 1) && (data <=  max_value))){                                                      
			input.push_back(data);
		} else { 
			cerr << "Tree can't be inside the house area !"; 
			exit(1); }                   
	}
}

void print(vector<int>& input){
	for (size_t i =0; i < input.size(); i++){
			cout << "Input " << input[i] << " ";
	}
    	cout << "\n";
}

int main(){
	int count = 0;
	string line;
	int data  = 0; 
	
	input(house);
	//print(house);
	
	reset();
	input_tree(trees, house);
	//print(trees);

	reset();
	input(fruits);
	//print(fruits);

	for (size_t i = 0; i < fruits.size(); i++){
		reset();
		fruit_loc.push_back(vector<int>());
		getline(cin, line);
		iss.str(line);  
		for (int j = 0; j < fruits[i]; j++){
			while (iss >> data){ 
				// constraints : -10^5  <= distances <= 10^5 
				if ((data >= min_value) && (data <=  max_value)){ 
					fruit_loc[i].push_back(data);
					// if a fruit has fallen on the house, count++
					if ((trees[0] + data >= house[i]) && (trees[i] + data <= house[1])) count++;
				} else { 
					cerr << "input is less than -10^5 or bigger than 10^5"; 
					exit(1); }   
			} 
		}		
		fruit_onsite.push_back(count);
	} 
	// print out numbers of fruits of each tree fall inside the house area
	for (auto value: fruit_onsite){
		cout << value << "\n";
	}
  			
	return 0;
}
Posted
Updated 22-Feb-22 23:08pm
v5
Comments
Richard MacCutchan 23-Feb-22 3:23am    
"failed some cases including this and this."
What does that mean? And those links just contain hundreds of numbers, with no explanation of what they represent or whether they are correct or not. Please use the Improve question link above, and add complete details of what is not working.
Patrice T 23-Feb-22 3:35am    
op have included a link to original challenge: https://www.hackerrank.com/challenges/apple-and-orange/problem[^]
Richard MacCutchan 23-Feb-22 3:36am    
So what? If people cannot explain clearly what the problem is they cannot expect us to figure out how to help.
lock&_lock 23-Feb-22 3:33am    
Hi, as I mentioned, they are sample test cases (therefore for input). I have edited my question for better understanding, thanks for pointing that out. "what they represent" : I have explained this in the Sample Input section, sample cases are the same with Sample Input section.
Richard MacCutchan 23-Feb-22 3:39am    
Well neither of those links contain anything remotely similar to your sample input, as described above. Nor have you explained what is wrong with the results you see.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900