0

Smaller Greater Equal Numbers PrepBuddy has N baskets containing one fruit each with some quality factor(ith basket have Ai quality factor) and Tina has one single basket with one fruit having quality factor K. She wants to know how many PrepBuddy's baskets have quality factor less(L) than K, how many baskets have quality factor more(M) than K and how many baskets have quality factor equal(E) to K.

Input format The first line contains an integer T, representing the number of test cases.T test cases follow,First linecontains two space-separated integers N and K.The second line contains N space-separated integers representing the quality factor of the basket.

Output format For each test case on a new line, print three space-separated integers representing the values of L, M,and E.

Constraints 1<=T<=100
1<=N,K<=10^5
−10^6<=A[i]<=10^6

Sum of all N over any test case file doesn't exceed 5∗10^6

Time Limit 1 second

Example Input
2
5 2
-1 0 -3 1 2
5 3
1 -1 -5 2 4

Output
4 0 1
4 1 0

Sample test case explanation In the first test case, K=2, the baskets with quality factor smaller than K are [1,2,3,4], there is no basket which has quality factor more than K and there is one basket [5] which have quality factor equal to K.

My solution

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

int main() {
  int t;
  cin >> t;
  while (t--) {
    ll n, k;
    cin >> n >> k;
    ll arr[n];
    for (ll i = 0; i < n; i++) {
      cin >> arr[i];
    }
    int less = 0, more = 0, equal = 0;
    for (ll i = 0; i < n; i++) {
      if (arr[i] < k) {
        less++;
      } else if (arr[i] > k) {
        more++;
      } else {
        equal++;
      }
      cout << less << " " << more << " " << equal << " ";
    }
    cout << endl;
  }
  return 0;
}

Input 2
5 2
-1 0 -3 1 2

5 3
1 -1 -5 2 4

Output 1 0 0 2 0 0 3 0 0 4 0 0 4 0 1

1 0 0 2 0 0 3 0 0 4 0 0 4 1 0

Why am I getting additional numbers like 1 0 0 2 0 0 3 0 0 4 0 0 along with my answer.How to correct this?? Please help

sweenish
  • 4,793
  • 3
  • 12
  • 23
  • 2
    Have you tried to debug your program with your favourite debugger? – 273K Oct 14 '20 at 15:32
  • 4
    If you indent the code properly, it should be easy to see. – user3386109 Oct 14 '20 at 15:35
  • You should narrow your question, since it is too long to read and understand. Also, try and divide the code so it will be more readable - readability is one of the most important things in coding! – Kerek Oct 14 '20 at 15:48
  • Having said the above, notice that both using `#include ` and `using namespace std;` are considered bad practices. You can read about it [here](https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H.) and [here](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Kerek Oct 14 '20 at 15:50

2 Answers2

1

The error in your code was quiet simple, and easy to debug. You were printing the output every run through the array, thus, getting all these extra prints.

How does it become easy to debug? Reorganizing the code so it will be more readable, made it quiet possible. Actually, the fix was moving the line:

cout<<less<<" "<<more<<" "<<equal<<" ";

Two line lower than it was. In order to demonstrate it, here is the code fixed and organized:

#include <iostream>
#include <vector>
#include <cstdint>

int main()
{
    int t; 
    std::cin >> t;
    
    while(t--)
    {
        std::int64_t n,k; 
        std::cin >> n >> k;
        std::vector<int> vec{n};
        
        for(std::size_t i = 0; i < n; ++i)
        {
            std::cin >> vec[i];
        }

        int less=0, more=0, equal=0;

        for (std::size_t i = 0; i < n; i++)
        {
            if (vec[i] < k)
            {
                less++;
            }
            else if (vec[i] > k)
            {
                more++;
            }
            else
            {
                equal++;
            }
            // The next output line was here, under the for loop!!!
        }
        std::cout << less << " " << more<< " " << equal << " "; // this is its place!
        
        std::cout << std::endl;
    }
    return 0; 
}

I have made only 3 changes:

  1. Removed the using namespace std; and #include <bit/stdc++.h>.
  2. Realigned the code according to its logical order - each new scope has its own indentation level, showing which command runs in which scope, revealing the mistake.
  3. Used proper types to each thing: In C++ there are no dynamic arrays in the format arr[n], thus, you need to use std::vector. Also, there are fixed size lengths in cstdint, and you should use those to ensure you are using the right type. Also, prefer using unsigned values when possible, for example, when indexing, use either std::size_t, std::uint32_t or std::uint64_t.
Kerek
  • 1,106
  • 1
  • 8
  • 18
0

This code worked fine for me

#include <bits/stdc++.h>
using namespace std;

int main()
{
  int T,N,K,arr[N];
  cin>>T;
  while(T--)
  {
    cin>>N;
    cin>>K;
    for(int i=0;i<N;i++)
    {
      cin>>arr[i];
    }
    int L=0,M=0,E=0;
    for(int i=0;i<N;i++)
    {
      if(arr[i]<K){
        L++;
      }
      else if(arr[i]>K)
      {
        M++;
      }
      else{
        E++;
      }
    }
    cout<<L<<" "<<M<<" "<<E<<endl;
  }
  
  return 0;
}
Keerthana
  • 1
  • 1
  • Recommending `#include ` in an answer on Stack Overflow is really asking for downvotes. See: [Why should I not #include ?](https://stackoverflow.com/q/31816095/10871073) Include the headers you need *explicitly*. Also worth reading: [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/10871073). – Adrian Mole May 15 '21 at 14:16