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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:48:03+00:00 2026-06-06T18:48:03+00:00

Possible Duplicate: Algorithm to find the total number of connected sets in a matrix

  • 0

Possible Duplicate:
Algorithm to find the total number of connected sets in a matrix

THE QUESTION:

https://amazonindia.interviewstreet.com/challenges/dashboard/#problem/4fd6570bd51e1

My solution is not passing all test cases.But I am not able to point out the scenarios where
my code will fail.
Can anyone point out whats wrong with my code?

My algorithm is simple that when you find a 1 make the value of that position equal to a variable called set incremented by 1. Initially set will be 1.
Then checking the neighbors of that position and if any of them is 1 make it equal to set+1.

Repeat the same till you reach the right bottom of matrix. Now increment set and repeat the process till any 1 is left in the matrix(I am deciding this using boolean left).

MY SOLUTION

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Solution {


    public static void main(String[] args) throws IOException {


               String no_of_tc;

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        no_of_tc=br.readLine();
        int[] result=new int[Integer.parseInt(no_of_tc)];
        int mdim=0,maxset=0;
        boolean left=false;
        int i=0;
        boolean first=true;
        //boolean anyneighbour=false;
        for(i=0;i<Integer.parseInt(no_of_tc);i++)
        {
            maxset=0;
            left=false;
            first=true;
            mdim=Integer.parseInt(br.readLine());

            int[][] matrix=new int[mdim][mdim];
            String[] values=new String[mdim];
            int valuecount=0,counter=0,countchar=0;

            for(valuecount=0;valuecount<mdim;valuecount++)
            {
                countchar=0;
                values[valuecount]=br.readLine();
                for(counter=0;counter<mdim;counter++)
                {
                    char temp=(values[valuecount].charAt(countchar));
                    countchar=countchar+2;
                    matrix[valuecount]counter]=Character.getNumericValue(temp);
                }

            }


            int j=0,k=0,set=1;
            while(j<mdim)
            {

                while(k<mdim)
                {
                if(first)
                {
                    if(matrix[j][k]==1)
                    {
                        matrix[j][k]=set+1;
                        first=false;
                        maxset=set;
                    }
                }
                if(matrix[j][k]==set+1)
                {
                    if((j-1>=0)&&(k+1<mdim)&&(matrix[j-1][k+1]==1))
                    {
                        matrix[j-1][k+1]=set+1;
                        //anyneighbour=true;
                        //proceedwhole=proceedwhole+1;
                    }
                    if((j-1>=0)&&matrix[j-1][k]==1)
                    {
                        matrix[j-1][k]=set+1;
                        //anyneighbour=true;
                        //proceedwhole=proceedwhole+1;
                    }
                    if((j-1>=0)&&(k-1>=0)&&matrix[j-1][k-1]==1)
                    {
                        matrix[j-1][k-1]=set+1;
                        //anyneighbour=true;
                        //proceedwhole=proceedwhole+1;
                    }
                    if((k+1<mdim)&&matrix[j][k+1]==1)
                    {
                        matrix[j][k+1]=set+1;
                        //anyneighbour=true;
                        //proceedwhole=proceedwhole+1;
                    }

                    if((k-1>=0)&&matrix[j][k-1]==1)
                    {
                        matrix[j][k-1]=set+1;
                        //anyneighbour=true;
                        //proceedwhole=proceedwhole+1;

                    }

                    if((j+1<mdim)&& (k+1<mdim) && matrix[j+1][k+1]==1)
                    {
                        matrix[j+1][k+1]=set+1;
                        //anyneighbour=true;
                        //proceedwhole=proceedwhole+1;

                    }
                    if((j+1<mdim)&&matrix[j+1][k]==1)
                    {
                        matrix[j+1][k]=set+1;
                        //anyneighbour=true;
                        //proceedwhole=proceedwhole+1;

                    }
                    if((j+1<mdim)&&(k-1>=0)&&matrix[j+1][k-1]==1)
                    {
                        matrix[j+1][k-1]=set+1;
                        //anyneighbour=true;
                        //proceedwhole=proceedwhole+1;

                    }

//                  if(anyneighbour==false&&proceedwhole==1)
//                  {
//                      first=true;
//                      set=set+1;
//              
//                      result[i]=result[i]+1;
//                      break;
//                  }
//                  


                }
                else
                {

                    if(matrix[j][k]==1)
                    {

                        left=true;

                    }

                }

                k++;

            }
                k=0;
                j++;


                if(j==mdim)
                {
                    if(left==true)
                    {
                        j=0;
                    first=true;
                        left=false;

                        set=set+1;

                    }
                    else
                    {
                        result[i]=maxset;
                    }


                }
//              if(anyneighbour==false&&left==true)
//              {
//                  j=0;
//                  left=false;
//                  
//              }
            }



        }
        int counter2=0;

        for(counter2=0;counter2<Integer.parseInt(no_of_tc);counter2++)
        {
            System.out.println(result[counter2]);


        }



    }

}
  • 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-06T18:48:07+00:00Added an answer on June 6, 2026 at 6:48 pm

    Here is one test case your code fails at:

    1
    3
    1 0 1
    1 0 1
    1 1 1
    

    There is only 1 component here.

    Also, your algorithm is slow. At every row you’re resetting j when there is a 1 not connected to the previous one. Try flood fill algorithm: http://en.wikipedia.org/wiki/Flood_fill

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

Sidebar

Related Questions

Possible Duplicate: Algorithm to find Lucky Numbers I came across this question.A number is
Possible Duplicate: Algorithm to find minimum number of weighings required to find defective ball
Possible Duplicate: Easy interview question got harder: given numbers 1..100, find the missing number(s)
Possible Duplicate: substring algorithm Given two strings, A and B, how to find the
Possible Duplicate: Algorithm for generating a random number is posible to generate a random
Possible Duplicate: optimal algorithm for finding unique divisors I have asked this question before,
Possible Duplicate: Solution to a recursive problem (code kata) give an algorithm to find
Possible Duplicate: Best algorithm to count the number of set bits in a 32-bit
Possible Duplicate: Best algorithm to count the number of set bits in a 32-bit
Possible Duplicate: Best algorithm to count the number of set bits in a 32-bit

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.