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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:10:03+00:00 2026-06-18T10:10:03+00:00

I wanted to know the Java code to find all the possible paths of

  • 0

I wanted to know the Java code to find all the possible paths of a multidimensional array. We should start from the [0][0] node and move forward. The thing to keep in mind that the paths must be in an increasing order irrespective of the elements order.
We should not traverse if the next element is equal to (or less then) the current one.

Example Array:

3   5   1
6   7   4
8   2   9

Result:

3,5,6,7,8
3,5,6,7,9
3,5,7,8
3,5,7,9
3,6,7,8
3,6,7,9
3,7,8
3,7,9
  • 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-18T10:10:05+00:00Added an answer on June 18, 2026 at 10:10 am

    I think the following code does what you describe. It start checking with the first node (0,0). For every node that is checked, a vector of neighbors is created. The neighbors are the nodes that are eligible to be a continuation to the path (i.e. neighboring nodes with higher value in the table). Then for each neighbor, the path is cloned, and the new neighbor is checked. This continues until the checked node has no eligible neighbors, at which point the path is printed and the algorithm terminates.

    Try this:

    import java.util.Arrays;
    import java.util.Vector;
    
    class Main {
      class Coords {
        int x;
        int y;
        Coords(int x, int y) {
          this.x = x;
          this.y = y;
        }
      }
      int [][] array = { {3,5,1},{6,7,4},{8,2,9}};
      Vector<Coords> getNeighbors(Coords coords) {
         int x = coords.x;
         int y = coords.y;
         Vector<Coords> result = new Vector<Coords>();
         if (x < array.length - 1) {
            if (array[x + 1][y] >= array[x][y])
              result.add(new Coords(x + 1, y));
         }
         if (x > 0) {
            if (array[x - 1][y] >= array[x][y])
              result.add(new Coords(x - 1, y));
         }
         if (y < array[x].length - 1) {
            if (array[x][y + 1] >= array[x][y])
              result.add(new Coords(x, y + 1));
         }
         if (y > 0) {
            if (array[x][y - 1] >= array[x][y])
              result.add(new Coords(x, y - 1));
         }
         if (x < (array.length - 1 ) &&  (y < array[x].length - 1)) {
            if (array[x + 1][y + 1] >= array[x][y])
              result.add(new Coords(x + 1, y + 1));
         }
         if (x < (array.length - 1 ) &&  (y > 0)) {
            if (array[x + 1][y - 1] >= array[x][y])
              result.add(new Coords(x + 1, y - 1));
         }
         if (x > 0 &&  (y < array[x].length - 1)) {
            if (array[x - 1][y + 1] >= array[x][y])
              result.add(new Coords(x - 1, y + 1));
         }
         if (x > 0 &&  y > 0) {
            if (array[x -1][y - 1] >= array[x][y])
              result.add(new Coords(x - 1, y - 1));
         }
         return result;
      }
      void checkNode(Vector<Integer> path, Coords coords) {
        path.add(array[coords.x][coords.y]);
        Vector<Coords> neighbors = getNeighbors(coords);
        if (neighbors.size() == 0) {
           for (Integer i : path) {
             System.out.print(i+"\t");
           }
           System.out.println();
        }
        for (Coords c : neighbors) {
          Vector<Integer> newpath = (Vector<Integer>) path.clone();
          checkNode(newpath, c);
        }
      }
      Main() {
        System.out.println ("Array: " + Arrays.deepToString(array));
        checkNode(new Vector<Integer>(),new Coords(0,0));
      }
    
      public static void main(String args[]) {
        new Main();
      }
    }
    

    Output:

    Array: [[3, 5, 1], [6, 7, 4], [8, 2, 9]]
    3   6   8   
    3   6   7   9   
    3   6   7   8   
    3   5   7   9   
    3   5   7   8   
    3   5   6   8   
    3   5   6   7   9   
    3   5   6   7   8   
    3   7   9   
    3   7   8   
    

    It also gives me the path 3,6,8, which is not in your sample output

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

Sidebar

Related Questions

My first thoughts are Erlang, or Java, but I wanted to know from others
Coming from a Java background, I find C++'s enums very lame. I wanted to
I wanted to know what changes should I be making to this code to
I wanted to know how does Java/C# scale up compared to C++ with reference
I wanted to know the existing Augmented Reality Frameworks in Java. I would like
We know that we can use a concept Java Package, but I just wanted
Wanted to know if there was a way one could query shelveset details from
Wanted to know if someone had a suggestion on code or maybe there's a
I wanted to know if it's possible to derive a method to generate a
Does anybody know of a good approach for testing Java 6 code in Java

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.