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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:26:49+00:00 2026-05-18T11:26:49+00:00

I am working on a project where at one point I am stuck. My

  • 0

I am working on a project where at one point I am stuck.

My question is for example I have the following 2D array containing 3 different integers.

2 2 2 2 1 
1 2 2 2 1 
3 3 2 3 2 
3 1 3 3 1 
1 1 2 3 1 
1 3 1 3 3 

What I want is to find the longest adjacent elements chain of array of any number contained in the array.

Like in the above array the longest chain is of digit 2.

2 2 2 2
  2 2 2
    2

Can anyone just guide me as to what I have to do to achieve this goal?

  • 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-05-18T11:26:49+00:00Added an answer on May 18, 2026 at 11:26 am

    Easier to draw than to explain…

    2 2 2 2 1 => A A A A B => (A: 4, B: 1)
    1 2 2 2 1 => C A A A B => (A: 3 + 4, B: 1 + 1, C: 1)
    3 3 2 3 2 => D D A E F => (A: 1 + 7, B: 2, C: 1, D: 2, E: 1, F: 1)
    3 1 3 3 1 => D G E E G => (A: 8, B: 2, C: 1, D: 2 + 1, E: 2 + 1, F: 1, G: 1)
    1 1 2 3 1 => ...
    1 3 1 3 3 => ...
    

    update:

    And now, with some real code:

    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    
    #define ROWS 6
    #define COLS 5
    
    unsigned char eles[ROWS][COLS] = { { 2, 2, 2, 2, 1 }, 
                                       { 1, 2, 2, 2, 1 }, 
                                       { 3, 3, 2, 3, 2 }, 
                                       { 3, 1, 3, 3, 1 }, 
                                       { 1, 1, 2, 3, 1 }, 
                                       { 1, 3, 1, 3, 3 } };
    
    struct zone {
      int acu;
      int row, col;
      int refs;
    };
    
    typedef struct zone zone;
    
    zone *
    new_zone(int row, int col) {
      zone *z = (zone *)malloc(sizeof(zone));
      z->col = col;
      z->row = row;
      z->refs = 1;
      z->acu = 0;
    }
    
    void croak (const char *str) {
      fprintf(stderr, "error: %s\n", str);
      exit(1);
    }
    
    void
    free_zone(zone *z) {
      if (z->refs != 0) croak("free_zone: reference count is not cero");
      free(z);
    }
    
    zone *
    ref_zone(zone *z) {
      z->refs++;
      return z;
    }
    
    void
    unref_zone(zone *z) {
      z->refs--;
      if (!z->refs) free_zone(z);
    }
    
    int
    main() {
      zone *last[COLS];
      zone *current[COLS];
      zone *best = new_zone(0, 0);
      int i, j;
      memset(last, 0, sizeof(last));
    
      for (j = 0; j < ROWS; j++) {
        for (i = 0; i < COLS; i++) {
          unsigned int ele = eles[j][i];
          zone *z;
          /* printf("analyzing ele: %d at row %d, col: %d\n", ele, j, i); */
          if (i && (ele == eles[j][i-1])) {
            /* printf("  equal to left element\n"); */
            z = ref_zone(current[i-1]);
            if (j && (ele == eles[j-1][i])) {
              zone *z1 = last[i];
              /* printf("  equal to upper element\n"); */
              if (z != z1) {
                int k;
                /* printf("  collapsing zone %p\n", z1); */
                z->acu += z1->acu;
                for (k = 0; k < COLS; k++) {
                  if (last[k] == z1) {
                    last[k] = ref_zone(z);
                    unref_zone(z1);
                  }
                }
                for (k = 0; k < i; k++) {
                  if (current[k] == z1) {
                    current[k] = ref_zone(z);
                    unref_zone(z1);
                  }
                }
              }
            }
          }
          else if (j && (ele == eles[j-1][i])) {
            /* printf("  equal to upper element\n"); */
            z = ref_zone(last[i]);
          }
          else {
            /* printf("  new element\n"); */
            z = new_zone(j, i);
          }
          z->acu++;
          current[i] = z;
          /* printf("  element zone: %p\n", z); */
        }
        for (i = 0; i < COLS; i++) {
          if (j) unref_zone(last[i]);
          last[i] = current[i];
          if (best->acu < current[i]->acu) {
            unref_zone(best);
            best = ref_zone(current[i]);
            /* printf("best zone changed to %p at row; %d, col: %d, acu: %d\n", best, best->row, best->col, best->acu); */
          }
        }
      }
      printf("best zone is at row: %d, col: %d, ele: %d, size: %d\n", best->row, best->col, eles[best->row][best->col], best->acu);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on project where I have the checkInternet method in one of
I'm working on a project in MVC 3 (first time using!). One question I
I'm currently working on a project in which at one point, the user may
I'm working on a project and I came to a point where the following
I am working on a project which at one point gets a list of
I am working on one project where there is a functionality need to implement
I am currently working on a project where one of our goals is to
I m working on project user to move the image in one position to
I'm currently working on a project with a one page design that'll slide up
While working a project tonight, I ended up using one .js resource file for

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.