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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:30:14+00:00 2026-05-22T03:30:14+00:00

Help with how to implement searching on n 2-dimenssional arrays. To be more specific:

  • 0

Help with how to implement searching on n 2-dimenssional arrays. To be more specific:
If I have 6 tables and I am putting these into a 2-dimensional array.I will provide a value say 10 like how val=0 here. I need to search from these tables all the combination values that make up 10. The value will be computed taking values from all these tables.

public static int Main() {
  int[] a = {2,1,4,7};
  int[] b = {3,-3,-8,0};
  int[] c = {-1,-4,-7,6};
  int sum;
  int i; int j;  int k;
  int val = 0;
  for(i = 0; i < 4; i++) {
    for(j = 0;j<4;j++) {
      for(k = 0;k<4;k++) {
        sum = a[i]* b[j]* c[k];

        if(sum == val)
          System.out.printf("%d  %d  %d\n",a[i],b[j],c[k]);
      }
    }
  }
}
  • 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-22T03:30:15+00:00Added an answer on May 22, 2026 at 3:30 am

    You can consider a table as list of values. Then, if you have N tables, your problem is to find lists of N integers (each one taken from one of the N tables) whose product is equal to a value p. You can solve the problem recursively:

    • given a non-empty list of tables {t1, t2, t3, ...}
    • given a product value p that you look for
    • for every value v in t1 you must look for solutions of the sub-problem with a product value p / v and and tables {t2, t3, ...} (this assumes that p % v == 0, because we’re dealing with integers
    • etc.

    Some java code below:

    public class SO6026472 {
    
        public static void main(String[] args) {
            // define individual tables
            Integer[] t1 = new Integer[] {2,-2,4,7};
            Integer[] t2 = new Integer[] {3,-3,-8,0};
            Integer[] t3 = new Integer[] {-1,-4,-7,6};
            Integer[] t4 = new Integer[] {1,5};
            // build list of tables
            List<List<Integer>> tables = new ArrayList<List<Integer>>();
            tables.add(Arrays.asList(t1));
            tables.add(Arrays.asList(t2));
            tables.add(Arrays.asList(t3));
            tables.add(Arrays.asList(t4));
            // find solutions
            SO6026472 c = new SO6026472();
            List<List<Integer>> solutions = c.find(36, tables);
            for (List<Integer> solution : solutions) {
                System.out.println(
                        Arrays.toString(solution.toArray(new Integer[0])));
            }
        }
    
        /**
         * Computes the ways of computing p as a product of elements taken from 
         * every table in tables.
         * 
         * @param p the target product value
         * @param tables the list of tables
         * @return the list of combinations of elements (one from each table) whose
         * product is equal to p
         */
        public List<List<Integer>> find(int p, List<List<Integer>> tables) {
            List<List<Integer>> solutions = new ArrayList<List<Integer>>();
            // if we have no tables, then we are done
            if (tables.size() == 0)
                return solutions;
            // if we have just one table, then we just have to check if it contains p
            if (tables.size() == 1) {
                if (tables.get(0).contains(p)) {
                    List<Integer> solution = new ArrayList<Integer>();
                    solution.add(p);
                    solutions.add(solution);
                    return solutions;
                } else
                    return solutions;
            }
            // if we have several tables, then we take the first table T, and for
            // every value v in T we search for (p / v) in the rest of the tables;
            // we do this only if p % v is equal to 0, because we're dealing with
            // ints
            List<Integer> table = tables.remove(0);
            for (Integer value : table) {
                if (value != 0 && p % value == 0) {
                    List<List<Integer>> subSolutions = find(p / value, tables);
                    if (! subSolutions.isEmpty()) {
                        for (List<Integer> subSolution : subSolutions) {
                            subSolution.add(0, value);
                        }
                        solutions.addAll(subSolutions);
                    }
                }
            }
            tables.add(0, table);
            return solutions;
        }
    
    }
    

    The code prints solutions for a slightly modified version of your example:

    [2, 3, 6, 1]
    [-2, -3, 6, 1]
    

    The solutions work for any number of tables. There are ways to improve the algorithm, for example by using memoization and dynamic programming. But I think that the recursive solution is more clear.

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

Sidebar

Related Questions

I am trying to implement the blackberry application which will help the user to
Can anyone help me suggesting which technologies will be useful to implement a ConnectedTv.
Someone please help. I have an interesting issue. I am trying to implement an
i have question for example i want to implement binary tree with array i
In a future project I will need to implement functionality meant for searching words
I have this problem: When I try to implement Ayende's complex searching found at:
In the application we are working on we are trying to implement Help. We
Im using the .NET framework 1.1 and Im hoping someone could help me implement
I'm new to CodeIgniter, and I need some help. I'd like to implement the
Help! I have an Axis web service that is being consumed by a C#

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.