Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9225351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:33:18+00:00 2026-06-18T04:33:18+00:00

I was just wondering if any other logic is possible for this problem: Question

  • 0

I was just wondering if any other logic is possible for this problem:
Question : Find the number of pairs in a given string and output the sum of all the pairs and the unpaired elements. PS: The input is case sensitive.

Example O/P:

eeqe 3

aaaa
2

rwertr
5

I figured out the solution to this problem by first sorting the input string and then comparing adjacent elements as shown in the code below:

int main()
{
int t,count=0,pos=0;
char swap;
    char a[201];
    cin>>a;
    int len=strlen(a);
    //cout<<a<<endl;
    for (int c = 0 ; c < ( len - 1 ); c++)
    {
for (int d = 0 ; d < len - c - 1; d++)
{
  if (a[d] > a[d+1]) /* For decreasing order use < */
  {
    swap       = a[d];
    a[d]   = a[d+1];
    a[d+1] = swap;
  }
}
}
    //cout<<a<<endl;
    count=0;
    for(int i=0;i<len;){
        if(a[i]==a[i+1]){
            count++;
            i+=2;
            //if(i== len-2)i++;
        }
        else{ count++; i++;}
    }
    //if(a[len-1]!=a[len-2])count++;
    cout<<count<<endl;
return 0;

}

This code works fine. But, I was just wondering if there is any other efficient solution to this problem that doesn’t involve sorting the entire input array.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:33:20+00:00Added an answer on June 18, 2026 at 4:33 am

    It basically avoids sorting based on the idea that there are only 256 possible chars, so it’s sufficent to count them.This is my solution:

    int main()
    {
      std::string s; std::cin >> s;
      int cnt[256] = {};
      for (std::size_t i = 0; i < s.size(); ++i)
        ++cnt[static_cast<unsigned char>(s[i])];
      int sum = 0;
      for (std::size_t i = 0; i < 256; ++i)
        sum += cnt[i]/2 + cnt[i]%2;
      std::cout << sum << std::endl;
    }
    

    If, for example, the string contains 5 times an 'a', this allows 5/2 pairs (integer division) and 1 remains unpaired (because 5 is odd => 5%2 is 1)

    Edit: Because we are here in SO:

    int main()
    {
      std::array<int, 256> cnt{-1}; // last char will be '\0', ignore this.
      std::for_each(std::istreambuf_iterator<char>(std::cin.rdbuf()),
                    std::istreambuf_iterator<char>{},
                    [&](unsigned char c){++cnt[c];});
      std::cout << std::accumulate(cnt.begin(), cnt.end(), 0,
                                   [](int i, int c)->int{return i+(c/2)+(c%2);}) << '\n';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wondering if you had any thoughts on this problem. I want to make
I was wondering if there was any decent way, other than NSLog-ing just about
I was just wondering if any one knows of a good script to emulate
I was just wondering if any one knew of any good beginner tutorials for
Just wondering is there any way I can check whether the url links to
i just wondering is there any performance issue or anything thing wrong if that
Just wondering if there is any way to get the NS records in C#.
Just wondering if there are any pitfalls using .net as the extension for a
Just wondering if anyone has any ideas on how we can style templates... Like
Just wondering if there is any benchmark software that I can download that will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.