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 9014301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:28:31+00:00 2026-06-16T03:28:31+00:00

i have written the following function //O(n^2) void MostCommonPair(char * cArr , char *

  • 0

i have written the following function

//O(n^2)
void MostCommonPair(char * cArr , char * ch1 , char * ch2 , int * amount)
{
    int count , max = 0;
    char cCurrent , cCurrent2;
    int i = 0 , j;
    while(*(cArr + i + 1) != '\0')
    {
        cCurrent = *(cArr + i);
        cCurrent2 = *(cArr + i + 1);
        for(j = i , count = 0 ; *(cArr + j + 1) != '\0' ; j++)
        {
            if(cCurrent ==  *(cArr + j) && cCurrent2 ==  *(cArr + j + 1))
            {
                count++;
            }
        }
        if(count > max)
        {
            *ch1 = cCurrent;
            *ch2 = cCurrent2;
            max = *amount = count;
        }
        i++;
    }
}

for the following input

“xdshahaalohalobscxbsbsbs”

ch1 = b ch2 = s amount = 4

but in my opinion the function is very un efficient , is there a way to go through the string only once or to reduce the run size to O(n)?

  • 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-16T03:28:32+00:00Added an answer on June 16, 2026 at 3:28 am

    Since char can hold up to 256 values, you can set up a two-dimensional table of [256*256] counters, run through your string once, incrementing the counter that corresponds to each pair of character in the string. Then you can go through the table of 256×256 numbers, pick the largest count, and know to what pair it belongs by looking at its position in the 2D array. Since the size of the counter table is fixed to a constant value independent of the length of the string, that operation is O(1), even though it requires two nested loops.

    int count[256][256];
    memset(count, 0, sizeof(count));
    const char *str = "xdshahaalohalobscxbsbsbs";
    for (const char *p = str ; *(p+1) ; p++) {
        count[(int)*p][(int)*(p+1)]++;
    }
    int bestA = 0, bestB = 0;
    for (int i = 0 ; i != 256 ; i++) {
        for (int j = 0 ; j != 256 ; j++) {
            if (count[i][j] > count[bestA][bestB]) {
                bestA = i;
                bestB = j;
            }
        }
    }
    printf("'%c%c' : %d times\n", bestA, bestB, count[bestA][bestB]);
    

    Here is a link to a demo on ideone.

    Keep in mind that although this is the fastest possible solution asymptotically (i.e. it’s O(N), and you cannot make it faster than O(N)) the performance is not going to be good for shorter strings. In fact, your solution will beat it hands-down on inputs shorter than approximately 256 characters, probably even more. There is a number of optimizations that you can apply to this code, but I decided against adding them on to keep the main idea of the code clearly visible in its purest and simplest form.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written following function public void TestSB() { string str = The quick
I have written the following function but it's isn't returning anything when I run
I have written the following algorithm in order to evaluate a function in MatLab
i have written the following code class Program { static void Main(string[] args) {
I have the following function that is used as the glutKeyboardFunc function parameter: void
I have a function in C++ such as: void* getField(interface* p) { int* temp
I have a following inline function: inline void normalizeGrayOutputCentredSigmoide(const type meanValue=(type)0.0, const type sensitivity=(type)2.0,
I'm writing some strings to a file using the following function... void writeText(const char*
i have the following function written in javascript which operate a slide up/down box.
I have the following function: public void rpSearchResults_ItemDataBound(Object Sender, RepeaterItemEventArgs e) { if (e.Item.ItemType

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.