Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am back with my hackerrank problem. I am trying to exercise with this question: Between two sets. I need to make two arrays of integers and determine all integers that satisfy the following two conditions:
  • The elements of the first array are all factors of the integer being considered (integer being considered % first array == 0)
  • The integer being considered is a factor of all elements of the second array (second array % integer being considered == 0)

These numbers are referred to as being between the two arrays and I need to determine how many such numbers exist. It should return the number of integers that are between the sets. Sample input:
Sample Input
2 3
2 4
16 32 96

Sample Output
3

My code actually passed the test case above, but for some reason, failed this one:
Sample Input
2 2
3 4
24 48

The expected output was 2. When I run my code on the website, it says that my output is 1, meanwhile when I run it locally in my machine, my output is exactly 2.

What I have tried:

I've been checking my code for hours, I'm not sure anymore which part needs correction since it's not even wrong in my machine and there's no error. Here's my code:
C++
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>

using namespace std;

vector<int> first_array;
vector<int> second_array;
vector<int> list_factors;
vector<int> results;

void input(){
	int a = 0;
	int b = 0;
	
	cin >> a >> b;

	cin.ignore(256, '\n'); 
	if ((a > 0) && (a < 11) && (b > 0) && (b < 11)){
		string input;
		int data = 0;
		getline(cin, input);
		istringstream iss(input);
		while (iss >> data){
			if ((data > 0) && (data < 101))
				first_array.push_back(data);		
		}
 
		input.clear(); 
		iss.clear();
		getline(cin, input);
		iss.str(input);
		while (iss >> data){
			if ((data > 0) && (data < 101))
				second_array.push_back(data);		
		}
	}
	else cout << "A and B is out of range" << endl;
}

void computation(vector<int> &first_array, vector<int> &second_array){
	int factor = 0;
	int count = 0; 
	
	for (int i = 0; i < second_array.size(); i++){
		for (int j = 0; j < first_array.size(); j++){
			if (second_array[i] > 0){
					if (second_array[i] % first_array[j] == 0){
						list_factors.push_back(second_array[i] / 
                                               first_array[j]);
					}
			}
		}
	}
	
	sort(list_factors.begin(), list_factors.end());
	list_factors.erase(unique(list_factors.begin(), 
                       list_factors.end()), list_factors.end());
	
	for (int k = 0; k < list_factors.size(); k++){
	bool first_condition_met = true;
	bool second_condition_met = true;
		for (int l = 0; l < first_array.size(); l++){
			if (list_factors[k] % first_array[l] != 0){
				first_condition_met = false; 
				break;
			}
		}
		
		if (first_condition_met){
			for (int m = 0; m < second_array.size(); m++){
				if (second_array[m] % list_factors[k] != 0){
					second_condition_met = false;
					break;
				}
			}
		}
          
		if (first_condition_met && second_condition_met) {
			results.push_back(list_factors[k]);	
            }				
	}
	
	cout << results.size() << endl;;	
}

int main(){
	input();
	computation(first_array, second_array);
	
	return 0;
}
Posted
Updated 6-Nov-23 3:59am
v2
Comments
Richard MacCutchan 25-Oct-23 8:20am    
I just ran that code, with the numbers you showed above, and the output was 1.

Just as Richard found, if I copy and paste your code verbatim into an online compiler - OnlineGDB.com[^] - and run it, then enter this text:
2 2
3 4
24 48
What do I get? "1", exactly the same as Richard.

So ... it's an initialization problem, or a compiler settings problem, or a 32 vs 64 bit machine problems, or ... anything really, that is different on your machine from those available to us.

Which means you need to debug the code on your machine, using the debugger to find out why you get 2 - and we can't do that for you because we don't get the same value as you! So grab your debugger and start looking at exactly what is happening in your code while it runs - it may be worth using your code on your machine in your debugger in parallel with the same code and inputs in the GDB debugger as that may make differences more apparent.

Good luck!
 
Share this answer
 
v3
Comments
lock&_lock 25-Oct-23 9:36am    
Oh no. Ok then, I will look into things you mentioned, thanks giving me some directions ! It's been decades since I studied GDB in school and I was never fully understand it to begin with, I guess it time for me to revisit.
OriginalGriff 25-Oct-23 10:08am    
You're welcome!
When I run this code I get 1 also. I think the key is your list_results vector. When I run this it has these values :
6
8
12
16
and the answer is obviously 1 because those numbers are factors of only one value : 12. This means you need to look carefully at how that vector is generated and determine if any values should be in it but are being missed. Maybe you should first determine mentally what other values should be in that vector. I can't think of any but I probably don't understand the problem well enough.
 
Share this answer
 

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