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:
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.