Help stuck in this problem

Help me in removing wrong answer in this program?
This is the problem:
There are N bottles. ith bottle has A[i] radius. Once a bottle is enclosed inside another bottle, it ceases to be visible. Minimize the number of visible bottles.

You can put ith bottle into jth bottle if following condition is fulfilled:

1) ith bottle itself is not enclosed in another bottle.

2) jth bottle does not enclose any other bottle.

3) Radius of bottle i is smaller than bottle j (i.e. A[i] < A[j]).
Input

8

1 1 2 3 4 5 5 4

Output

2
Explanation:
1st bottle can be kept in 3 rd one 1-->2 , which makes following bottles visible [1,2,3,4,5,5,4]

similarly after following operations, the following will be the corresponding visible bottles

Operation ? Visible Bottles

2 ? 3 [1,3,4,5,5,4]

3 ? 4 [1,4,5,5,4]

4 ? 5 [1,5,5,4]

1 ? 4 [5,5,4]

4 ? 5 [5,5]

finally there are 2 bottles which are visible. Hence, the answer is 2




#include <iostream>
#include <algorithm>

using namespace std;

int main(){
int n;
cin>>n;
int a[n], b[n], i, count=0;
for(i=0;i<n;i++){

cin>>a[i];
b[i]=1;

}
sort(a, a+n);
for(i=0;i<n;i++){

for(int j = i+1;j<n;j++){

if(a[i]<a[j] && b[j] == 1){

count = count+1;
b[j] = 0;
break;
}

}

}
int result = n-count;
cout<<endl<<result;


}
Hello counter strike,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


Your post shows as reported, but I have no idea why.

As a start, in general, a VLA (variablelength array) is not allowed in C++ although there are some older compilers that will allow this.

Try to avoid a single name variable. This can become confusing and hard to follow. Using a single letter variable in a for loop is OK mist of the time because these variables are local to the for loop and easy to keep track of.

The following code should give you some ideas of what you could do. This is based on what you started with.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <algorithm>

//using namespace std;  // <--- Best not to use.

int main()
{
	constexpr size_t MAXSIZE{ 20 };

	int arrayUsed{};
	int numbers[MAXSIZE]{}, unknownUse[MAXSIZE]{}, i{}, count = 0;
	
	std::cout << "\n Needs a prompt!: ";
	std::cin >> arrayUsed;

	if (arrayUsed >= MAXSIZE)
	{
		std::cout << "\n    Size to large. Must be 20 or less.\n" << std::endl;

		std::cout << "\n Needs a prompt!: ";
		std::cin >> arrayUsed;
	}

	std::cout << '\n';

	for (i = 0; i < arrayUsed; i++)
	{
		std::cout << " Enter number " << i + 1 << ": ";
		std::cin >> numbers[i];
		unknownUse[i] = 1;
	}

	std::sort(numbers, numbers + arrayUsed);

	for (i = 0; i < arrayUsed; i++)
	{
		for (int j = i + 1; j < arrayUsed; j++)
		{
			if (numbers[i] < numbers[j] && unknownUse[j] == 1)
			{
				count = count + 1;
				unknownUse[j] = 0;
				break;
			}
		}
	}

	//int result = arrayUsed - count;

	//std::cout << std::endl << result;

	std::cout << "\n The result is: " << arrayUsed - count << std::endl;  // <--- Your previous two lines can be done this way.

	return 0;  // <--- Not required, but does come in handy. Also a good place for a break point.
}

Indentation is very helpful and the proper use of blank lines makes it easier to read.

After running the program a couple of times even with your input data I have a difficult time understand what the second array is for. You change the values of the elements you call "b", but never use it beyond the final for loop.

Given your test data of:

8

1 1 2 3 4 5 5 4


It did produce an answer of (2). Given the above input I am taking that the final output is (2).

Help me in removing wrong answer in this program?
What wrong answers? where are they? Your question is gormless and only leads to more questions.

Looking at your code I have no idea what is producing a wrong answer or where it is at this time.

Hope that helps,

Andy
Topic archived. No new replies allowed.