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

  • Home
  • SEARCH
  • 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 6090299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:11:55+00:00 2026-05-23T12:11:55+00:00

Hello I am working on java and I have to generate all the possible

  • 0

Hello

I am working on java and I have to generate all the possible patterns(combinations) of M-by-N matrices such that in the same row there should not be more than a single 1, Same column may contain more than a single 1, Taking an example of 3*3 matrix, matrices generated should look like:

1 0 0  
1 0 0  
1 0 0  

0 1 0  
1 0 0  
1 0 0  

0 0 1  
1 0 0  
1 0 0  

1 0 0  
0 1 0  
1 0 0  

1 0 0  
0 0 1  
1 0 0  

1 0 0  
1 0 0  
0 1 0  

1 0 0  
1 0 0  
0 0 1  

0 1 0  
0 1 0  
0 1 0  

1 0 0  
0 1 0  
0 1 0  

0 0 1  
0 1 0  
0 1 0  

0 1 0  
1 0 0  
0 1 0  

0 1 0  
0 0 1  
0 1 0  

….. and so on.

As I have already said that progrom should be flexible that can generate all such possible patterns for any value of M and N.

Please help me..

Thanks!

  • 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-23T12:11:55+00:00Added an answer on May 23, 2026 at 12:11 pm

    Here’s one solution. ideone.com demo.

    I bet it can be done with half the number of rows though.

    public static Set<int[][]> chooseFrom(List<int[]> choices, int choose) {
        Set<int[][]> results = new HashSet<int[][]>();
        if (choose == 1) {
            for (int[] choice : choices)
                results.add(new int[][] { choice });
        } else {
            for (int[] choice : choices) {
                for (int[][] submatrix : chooseFrom(choices, choose - 1)) {
                    int[][] matrix = new int[choose][];
                    matrix[0] = choice;
                    for (int r = 1; r < choose; r++)
                        matrix[r] = submatrix[r-1];
                    results.add(matrix);
                }
            }
        }
    
        return results;
    }
    
    public static Set<int[][]> matrices(int M, int N) {
        List<int[]> possibleRows = new ArrayList<int[]>();
    
        for (int i = 0; i < N; i++) {
            int[] row = new int[N];
            row[i] = 1;
            possibleRows.add(row);
        }
    
        return chooseFrom(possibleRows, M);
    }
    

    Output for the 2×3 case:

    [ 0 0 1 ]
    [ 0 1 0 ]
    
    [ 1 0 0 ]
    [ 0 0 1 ]
    
    [ 0 0 1 ]
    [ 0 0 1 ]
    
    [ 0 0 1 ]
    [ 1 0 0 ]
    
    [ 0 1 0 ]
    [ 1 0 0 ]
    
    [ 0 1 0 ]
    [ 0 1 0 ]
    
    [ 1 0 0 ]
    [ 1 0 0 ]
    
    [ 0 1 0 ]
    [ 0 0 1 ]
    
    [ 1 0 0 ]
    [ 0 1 0 ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I have this list that i am working on. When i move the
Hello Ruby/Rails/Merb developers! Im currently working on a web project that will have a
So I have been working on a 2 player Tic-Tac-Toe game in java that
I have a working exec(java $file) command in php, but the problem is that
Hello I am working with a simulator that uses rcS scripts to boot, this
HELLO, I am working on XCODE 3.1.4 , my iphone have 3.1.2 os...But as
hello i have created one windows application in c# .net and its working fine
I have an Axis (1.4) web service (running on Tomcat 6.0.20) that is working
hello I have some question about writing class in Java, why this one is
Working with chrome on ubuntu, have a basic java applet and javascript interaction. The

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.