-3

This is a HackerRank Problem ,I have written my code in c++ , but I am not getting correct output:

Here's the problem:

There is a large pile of socks that must be paired by color. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are.

n = 10

arr = [1,1,3,1,2,1,3,3,3,3]

Output: 4

Explation: There is two pair of color 1 and two of color 3. There are two odd socks left, one of each color. The number of pairs is 4.

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++)
       cin>>arr[i];
    sort(arr,arr+n);
    int sum=1,count=0;
    for(int i=0;i<n-1;i++)
    {
        if(arr[i]==arr[i+1])
           sum += 1;
        else
        {
            count += sum/2;
            sum = 1;
        }
    }
    cout<<count;
}

My output : 2.

Correct output : 4

Paul R
  • 208,748
  • 37
  • 389
  • 560
Krishna
  • 3
  • 1
  • 1
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a [debugger](https://en.wikipedia.org/wiki/Debugger) to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Paul R Feb 23 '21 at 09:50
  • You are missing counting the last `sum`. Add an addition `+= sum/2` after the `for` loop. – Damien Feb 23 '21 at 10:02
  • 2
    Aren't these challenges meant to improve **your** abilities as you solve them? Posting them here seems pointless for all concerned. –  Feb 23 '21 at 10:02
  • you are counting only adjacent socks, but nothing in the problem statement says that the pairs must be adjacent. – 463035818_is_not_an_ai Feb 23 '21 at 10:03
  • 1
    @largest_prime_is_463035818 The array is sorted before counting the pairs. – Damien Feb 23 '21 at 10:03
  • 1
    for more details on pronouns comment: [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). – 463035818_is_not_an_ai Feb 23 '21 at 10:14

1 Answers1

0

You are just missing to take into account the last sum.
An addition += sum/2 must be added after the for loop.

#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
    int n;
    std::cin >> n;
    std::vector<int> arr(n);
    for(int i=0; i<n; i++) {
       std::cin >> arr[i];
    }
    std::sort(arr.begin(), arr.end());
    int sum = 1,count = 0;
    for(int i=0; i<n-1; i++)
    {
        if(arr[i] == arr[i+1]) {
            sum += 1;
        } else {
            count += sum/2;
            sum = 1;
        }
    }
    count += sum/2;
    std::cout << count;
}
Damien
  • 4,809
  • 4
  • 15
  • 20