-1

Suppose there's an 1D array test[]={1,2,3} and a 2D array arr1[3][5]={{1,2,5,4,3},{3,7,1,4,2},{2,9,7,8,3}}.

What is required as an output is like:

test is the subset of row 0 of arr1
test is the subset of row 1 of arr1
test is not the subset of row 2 of arr1

Here's the code I have implemented so far:

class GFG {
    public static void main(String args[]) {
        int arr1[][] = { { 11, 1, 13, 3, 7 }, 
                         { 11, 1, 17, 7, 3 }, 
                         { 2, 5, 8, 9, 10 } };

        int test[] = { 11, 3, 7, 1 };

        int m = arr1.length; // rows
        int n = test.length;
        int o = arr1[0].length; // no. of elements in each row

        System.out.println(o); // just for testing if it works

        int i = 0;
        int j = 0;
        int k = 0;

        for (i = 0; i < n; i++) {
            for (j = 0; j < m && j != m; j++) {
                for (k = 0; k < o; k++)
                    if (test[i] == arr1[j][k])
                        break;

                if (k == o)
                    System.out.println("test[] is " + "not a subset of arr1 " + j + " row");
                else
                    System.out.println("test[] is " + "subset of arr1 " + j + " row");
            }
        }
    }
}

But the output which I'm getting out of this is:

output here

I realize that's the i loop working to print it repeatedly, still I didn't get a satisfactory output in this case.

What can be done here? or is there is a much optimal implementation to this problem? Any suggestions welcome.

2 Answers2

1

You messed up orders of cycles: you should iterate over arr first. The idea is ti use flags: isSubset, which becomes false when some element is not found in a row, contains, which becomes true if currently checked element is in a row.

There are improvements which can be made (like foreach loops and labeled breaks), but I decided to keep code simple.

public class GFG {
    public static void main(String args[]) {
        int arr[][] = { { 11, 1, 13, 3, 7 }, 
                         { 11, 1, 17, 7, 3 }, 
                         { 2, 5, 8, 9, 10 } };

        int test[] = { 11, 3, 7, 1 };


        for (int i = 0; i < arr.length; i++) {
            boolean isSubset = true;
            for (int j = 0; j < test.length; j++) {
                boolean contains = false;
                for (int k = 0; k < arr[i].length; k++) {
                    if (test[j] == arr[i][k]) {
                        contains = true;
                        break;
                    }
                }
                if (!contains) {
                    isSubset = false;
                    break;
                }
            }
            if (isSubset) {
                System.out.println("test[] is " + "subset of arr " + i + " row");
            } else {
                System.out.println("test[] is " + "not a subset of arr " + i + " row");
            }
        }
    }
}
0

Try to decompose your solution to smaller methods, so your code will be clearer and you can easier think of what is going on. Here is an example how it can be done:

class GFG {
  public static void main(String args[]) {
    int arr1[][] = { { 11, 1, 13, 3, 7 },
          { 11, 1, 17, 7, 3 },
          { 2, 5, 8, 9, 10 } };
    int test[] = { 11, 3, 7, 1 };

    System.out.println(arr1[0].length); // just for testing if it works

    for (int i = 0; i < arr1.length; i++) {
      if (isSubset(test, arr1[i])) {
        System.out.println("test[] is " + "subset of arr1 " + i + " row");
      } else {
        System.out.println("test[] is " + "not a subset of arr1 " + i + " row");
      }
    }
  }

  /* returns true if arr contains all elements of sub */
  static boolean isSubset(int[] sub, int[] arr) {
    for (int e : sub) {
      if (!contains(e, arr)) return false;
    }
    return true;
  }

  /* returns true if arr contains elem */
  static boolean contains(int elem, int[] arr) {
    for (int e : arr) {
      if (elem == e) return true;
    }
    return false;
  }
}
Donat
  • 4,157
  • 3
  • 11
  • 26