Array Subset of another array

Given two arrays: a1[0..n-1] of size n and a2[0..m-1] of size m. Task is to check whether a2[] is a subset of a1[] or not. Both the arrays can be sorted or unsorted..









STUDYEVERTHING





// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;

string isSubset(int a1[], int a2[], int n, int m) ;

int main() {
    int t;
    cin >> t;

    while (t--) {
        int n, m;
        cin >> n >> m;
        int a1[n], a2[m];

        for (int i = 0; i < n; i++) {
            cin >> a1[i];
        }
        for (int i = 0; i < m; i++) {
            cin >> a2[i];
        }

        cout << isSubset(a1, a2, n, m) << endl;
    }
    return 0;
}
// } Driver Code Ends


string isSubset(int a1[], int a2[], int n, int m) {
    string s;
    int j=0, count=0;
    for(int j=0; j<m; j++){
    for(int i=0; i<n; i++){
          if(a1[i]==a2[j]){
              count++;
          }
    }
    }
    if(count == m){
        s = "Yes";
    }
    else {
        s= "No";
    }
    return s;
}




Happy Coding 😊.





Post a Comment

If you have furthur QUERIES please let us know

Previous Post Next Post