Open In App

Find whether an array is subset of another array

Last Updated : 05 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Solve Problem
Basic
44.05%
3.8L

Given two arrays arr1[] and arr2[] of size m and n respectively, the task is to determine whether arr2[] is a subset of arr1[]. Both arrays are not sorted, and elements are distinct.

Examples: 

Input: arr1[] = {11, 1, 13, 21, 3, 7}, arr2[] = {11, 3, 7, 1} 
Output: Yes

Input: arr1[] = {1, 2, 3, 4, 5, 6}, arr2[] = {1, 2, 4} 
Output: Yes

Input: arr1[] = {10, 5, 2, 23, 19}, arr2[] = {19, 5, 3} 
Output: No

Approaches to find whether an array is subset of another array:

[Naive approach] Using Nested Loops – O(m*n) time and O(1) auxiliary space

The very basic approach is to use two nested loops: the outer loop picks each element from arr2[], and the inner loop searches for this element in arr1[]. If an element from arr2[] is not found in arr1[] then returns false. If all elements in arr2[] are found in arr1[], the function returns true.

Code Implementation:

C++
#include<bits/stdc++.h>
using namespace std;

// Checks if an array is a subset of another array.
bool isSubset(vector<int> & arr1, vector<int> & arr2, int m, int n) {
  
    // Iterate over each element in the second array
    for (int i = 0; i < n; i++) {
        bool found = false;
      
        // Check if the element exists in the first array
        for (int j = 0; j < m; j++) {
            if (arr2[i] == arr1[j]) {
                found = true;
                break;
            }
        }
      
        // If any element is not found, return false
        if (!found) return false;
    }
  
    // If all elements are found, return true
    return true;
}

int main() {
    vector<int> arr1 = {11, 1, 13, 21, 3, 7};
    vector<int> arr2 = {11, 3, 7, 1};
    int m = arr1.size();
    int n = arr2.size();

    if (isSubset(arr1, arr2, m, n)) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }

    return 0;
}
Java
public class SubsetChecker {

    public static boolean isSubset(int[] arr1, int[] arr2, int m, int n) {
        // Iterate over each element in the second array
        for (int i = 0; i < n; i++) {
            boolean found = false;
            // Check if the element exists in the first array
            for (int j = 0; j < m; j++) {
                if (arr2[i] == arr1[j]) {
                    found = true;
                    break;
                }
            }
            // If any element is not found, return false
            if (!found) return false;
        }
        // If all elements are found, return true
        return true;
    }

    public static void main(String[] args) {
        int[] arr1 = {11, 1, 13, 21, 3, 7};
        int[] arr2 = {11, 3, 7, 1};
        int m = arr1.length;
        int n = arr2.length;

        if (isSubset(arr1, arr2, m, n)) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}
Python
def is_subset(arr1, arr2, m, n):

    # Iterate over each element in the second array
    for i in range(n):
        found = False

        # Check if the element exists in the first array
        for j in range(m):
            if arr2[i] == arr1[j]:
                found = True
                break

        # If any element is not found, return false
        if not found:
            return False

    # If all elements are found, return true
    return True


if __name__ == "__main__":
    arr1 = [11, 1, 13, 21, 3, 7]
    arr2 = [11, 3, 7, 1]
    m = len(arr1)
    n = len(arr2)

    if is_subset(arr1, arr2, m, n):
        print("Yes")
    else:
        print("No")
JavaScript
function isSubset(arr1, arr2, m, n) {

  // Iterate over each element in the second array
  for (let i = 0; i < n; i++) {
    let found = false;
    
    // Check if the element exists in the first array
    for (let j = 0; j < m; j++) {
      if (arr2[i] === arr1[j]) {
        found = true;
        break;
      }
    }
    
    // If any element is not found, return false
    if (!found) return false;
  }
  
  // If all elements are found, return true
  return true;
}

const arr1 = [11, 1, 13, 21, 3, 7];
const arr2 = [11, 3, 7, 1];
const m = arr1.length;
const n = arr2.length;

if (isSubset(arr1, arr2, m, n)) {
  console.log("Yes");
} else {
  console.log("No");
}

Output
Yes

Time Complexity: O(m*n)
Auxiliary Space: O(1)

[Optimized Approach] Using Sorting & Two Pointers – O(m log m + n log n) time and O(1) auxiliary Space

An efficient method involves sorting both arrays and using a two-pointer technique. By sorting both arrays, we can traverse them simultaneously with two pointers. One pointer starts at the beginning of arr1[] and the other at the beginning of arr2[]. If the current element in arr1[] matches the current element in arr2[], we move both pointers forward. If the current element in arr1[] is smaller, we move the pointer in arr1[] forward to find a potential match. If the current element in arr1[] is larger, it means the element in arr2[] is not in arr1[], and we return false. If we successfully traverse all elements in arr2[], we return true.

Code Implementation:

C++
#include <bits/stdc++.h>
using namespace std;

bool isSubset(vector<int>& arr1, vector<int>& arr2)
{

    // Sort both arrays
    sort(arr1.begin(), arr1.end());
    sort(arr2.begin(), arr2.end());

    int i = 0, j = 0;

    // Traverse both arrays using two pointers
    while (i < arr1.size() && j < arr2.size()) {
        if (arr1[i] < arr2[j]) {
            i++;
        }
        else if (arr1[i] == arr2[j]) {
            i++;
            j++;
        }
        else {
            // If element in arr2 is not found in arr1
            return false;
        }
    }

    // If we have traversed all elements in arr2, it is a
    // subset
    return (j == arr2.size());
}

int main()
{
    vector<int> arr1 = { 11, 1, 13, 21, 3, 7 };
    vector<int> arr2 = { 11, 3, 7, 1 };

    if (isSubset(arr1, arr2)) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }

    return 0;
}
Python
def is_subset(arr1, arr2):

    # Sort both arrays in ascending order
    arr1.sort()
    arr2.sort()

    i = 0
    j = 0

    # Traverse both arrays using two pointers
    while i < len(arr1) and j < len(arr2):
        if arr1[i] < arr2[j]:
            # Element in arr1 is smaller, move to the next element in arr1
            i += 1
        elif arr1[i] == arr2[j]:
            # Element found in both arrays, move to the next element in both arrays
            i += 1
            j += 1
        else:
            # Element in arr2 not found in arr1, not a subset
            return False

    # If we have traversed all elements in arr2, it is a subset
    return j == len(arr2)


# Example usage
arr1 = [11, 1, 13, 21, 3, 7]
arr2 = [11, 3, 7, 1]

if is_subset(arr1, arr2):
    print("Yes")
else:
    print("No")
C#
using System;
using System.Collections.Generic;
using System.Linq;

public class SubsetChecker {
    public static bool IsSubset(List<int> arr1,
                                List<int> arr2)
    {
        // Sort both arrays in ascending order
        arr1.Sort();
        arr2.Sort();

        int i = 0;
        int j = 0;

        // Traverse both arrays using two pointers
        while (i < arr1.Count && j < arr2.Count) {
            if (arr1[i] < arr2[j]) {
                // Element in arr1 is smaller, move to the
                // next element in arr1
                i++;
            }
            else if (arr1[i] == arr2[j]) {
                // Element found in both arrays, move to the
                // next element in both arrays
                i++;
                j++;
            }
            else {
                // Element in arr2 not found in arr1, not a
                // subset
                return false;
            }
        }

        // If we have traversed all elements in arr2, it is
        // a subset
        return j == arr2.Count;
    }

    public static void Main(string[] args)
    {
        List<int> arr1
            = new List<int>() { 11, 1, 13, 21, 3, 7 };
        List<int> arr2 = new List<int>() { 11, 3, 7, 1 };

        if (IsSubset(arr1, arr2)) {
            Console.WriteLine("Yes");
        }
        else {
            Console.WriteLine("No");
        }
    }
}
JavaScript
function isSubset(arr1, arr2) {
    // Sort both arrays in ascending order
    arr1.sort((a, b) => a - b);
    arr2.sort((a, b) => a - b);

    let i = 0;
    let j = 0;

    // Traverse both arrays using two pointers
    while (i < arr1.length && j < arr2.length) {
        if (arr1[i] < arr2[j]) {
            // Element in arr1 is smaller, move to the next element in arr1
            i++;
        } else if (arr1[i] === arr2[j]) {
            // Element found in both arrays, move to the next element in both arrays
            i++;
            j++;
        } else {
            // Element in arr2 not found in arr1, not a subset
            return false;
        }
    }

    // If we have traversed all elements in arr2, it is a subset
    return j === arr2.length;
}

// Example usage
const arr1 = [11, 1, 13, 21, 3, 7];
const arr2 = [11, 3, 7, 1];

if (isSubset(arr1, arr2)) {
    console.log("Yes");
} else {
    console.log("No");
}

Output
Yes

Time Complexity: O(m log m + n log n)
Auxiliary Space: O(1)

[Expected Approach] Using Hashing – O(m + n) time and O(m) auxiliary space

We can use a hash set to store elements of arr1[], this will help us in constant time complexity searching. We first insert all elements of arr1[] into a hash set. Then, for each element in arr2[], we check if it exists in the hash set. If any element in arr2[] is not found in the hash set, we return false. Otherwise, if all elements are found, we return true.

Code Implementation:

C++
#include <bits/stdc++.h>
using namespace std;

bool isSubsetUsingHashing(const vector<int>& arr1, const vector<int>& arr2) {

    // Create a hash set and insert all elements of arr1
    unordered_set<int> hashSet(arr1.begin(), arr1.end());
    
    // Check each element of arr2 in the hash set
    for (int num : arr2) {
        if (hashSet.find(num) == hashSet.end()) {
            return false;
        }
    }
    
    // If all elements of arr2 are found in the hash set
    return true;
}

// Driver code
int main() {
    vector<int> arr1 = {1, 2, 3, 4, 5, 6, 7, 8};
    vector<int> arr2 = {1, 2, 3, 1};
    
    if (isSubsetUsingHashing(arr1, arr2)) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
    
    return 0;
}
Java
import java.util.HashSet;
import java.util.Set;

public class SubsetChecker {

    public static boolean isSubsetUsingHashing(int[] arr1,
                                               int[] arr2)
    {
        // Create a hash set and insert all elements of arr1
        Set<Integer> hashSet = new HashSet<>();
        for (int num : arr1) {
            hashSet.add(num);
        }

        // Check each element of arr2 in the hash set
        for (int num : arr2) {
            if (!hashSet.contains(num)) {
                return false;
            }
        }

        // If all elements of arr2 are found in the hash set
        return true;
    }

    public static void main(String[] args)
    {
        int[] arr1 = { 11, 1, 13, 21, 3, 7 };
        int[] arr2 = { 11, 3, 7, 1 };

        if (isSubsetUsingHashing(arr1, arr2)) {
            System.out.println("Yes");
        }
        else {
            System.out.println("No");
        }
    }
}
Python
def is_subset_using_hashing(arr1, arr2):

    # Create a hash set and insert all elements of arr1
    hash_set = set(arr1)

    # Check each element of arr2 in the hash set
    for num in arr2:
        if num not in hash_set:
            return False

    # If all elements of arr2 are found in the hash set
    return True


# Driver code
arr1 = [11, 1, 13, 21, 3, 7]
arr2 = [11, 3, 7, 1]

if is_subset_using_hashing(arr1, arr2):
    print("Yes")
else:
    print("No")
C#
using System;
using System.Collections.Generic;

public class SubsetChecker
{
    public static bool IsSubsetUsingHashing(List<int> arr1, List<int> arr2)
    {
        // Create a hash set and insert all elements of arr1
        HashSet<int> hashSet = new HashSet<int>(arr1);

        // Check each element of arr2 in the hash set
        foreach (int num in arr2)
        {
            if (!hashSet.Contains(num))
            {
                return false;
            }
        }

        // If all elements of arr2 are found in the hash set
        return true;
    }

    public static void Main(string[] args)
    {
        List<int> arr1 = new List<int>() { 11, 1, 13, 21, 3, 7 };
        List<int> arr2 = new List<int>() { 11, 3, 7, 1 };

        if (IsSubsetUsingHashing(arr1, arr2))
        {
            Console.WriteLine("Yes");
        }
        else
        {
            Console.WriteLine("No");
        }
    }
}
JavaScript
function isSubsetUsingHashing(arr1, arr2) {

    // Create a hash set and insert all elements of arr1
    const hashSet = new Set(arr1);

    // Check each element of arr2 in the hash set
    for (const num of arr2) {
        if (!hashSet.has(num)) {
            return false;
        }
    }

    // If all elements of arr2 are found in the hash set
    return true;
}

// Driver code
const arr1 = [11, 1, 13, 21, 3, 7];
const arr2 = [11, 3, 7, 1];

if (isSubsetUsingHashing(arr1, arr2)) {
    console.log("Yes");
} else {
    console.log("No");
}

Output
Yes

Time Complexity: O(m + n), where m and n are the size of arr1 and arr2 respectively.
Auxiliary Space: O(m)






Previous Article
Next Article

Similar Reads

Find whether an array is subset of another array using Map
Given two arrays: arr1[0..m-1] and arr2[0..n-1]. Find whether arr2[] is a subset of arr1[] or not. Both the arrays are not in sorted order. It may be assumed that elements in both arrays are distinct.Examples: Input: arr1[] = {11, 1, 13, 21, 3, 7}, arr2[] = {11, 3, 7, 1} Output: arr2[] is a subset of arr1[] Input: arr1[] = {1, 2, 3, 4, 5, 6}, arr2[
7 min read
Find a non empty subset in an array of N integers such that sum of elements of subset is divisible by N
Given an array of N integers, the task is to find a non-empty subset such that the sum of elements of the subset is divisible by N. Output any such subset with its size and the indices(1-based indexing) of elements in the original array if it exists. Prerequisites: Pigeonhole PrincipleExamples: Input: arr[] = { 2, 3, 7, 1, 9 } Output: 2 1 2 The req
8 min read
Find maximum subset sum formed by partitioning any subset of array into 2 partitions with equal sum
Given an array A containing N elements. Partition any subset of this array into two disjoint subsets such that both the subsets have an identical sum. Obtain the maximum sum that can be obtained after partitioning. Note: It is not necessary to partition the entire array, that is any element might not contribute to any of the partition. Examples: In
15+ min read
Largest possible Subset from an Array such that no element is K times any other element in the Subset
Given an array arr[] consisting of N distinct integers and an integer K, the task is to find the maximum size of a subset possible such that no element in the subset is K times any other element of the subset(i.e. no such pair {n, m} should be present in the subset such that either m = n * K or n = m * K). Examples: Input: arr[] = {2, 8, 6, 5, 3},
7 min read
Split Array into K non-overlapping subset such that maximum among all subset sum is minimum
Given an array arr[] consisting of N integers and an integer K, the task is to split the given array into K non-overlapping subsets such that the maximum among the sum of all subsets is minimum. Examples: Input: arr[] = {1, 7, 9, 2, 12, 3, 3}, M = 3Output: 13Explanation:One possible way to split the array into 3 non-overlapping subsets is {arr[4],
7 min read
Sum of maximum and minimum of Kth subset ordered by increasing subset sum
Given an integer N and a set of all non-negative powers of N as S = {N0, N1, N2, N3, ... }, arrange all non-empty subsets of S in increasing order of subset-sum. The task is to find the sum of the greatest and smallest elements of the Kth subset from that ordering. Examples: Input: N = 4, K = 3Output: 5Explanation: S = {1, 4, 16, 64, ...}.Therefore
7 min read
Maximum size of subset such that product of all subset elements is a factor of N
Given an integer N and an array arr[] having M integers, the task is to find the maximum size of the subset such that the product of all elements of the subset is a factor of N. Examples: Input: N = 12, arr[] = {2, 3, 4}Output: 2Explanation: The given array 5 subsets such that the product of all elements of the subset is a factor of 12. They are {2
6 min read
Minimum Subset sum difference problem with Subset partitioning
Given a set of N integers with up to 40 elements, the task is to partition the set into two subsets of equal size (or the closest possible), such that the difference between the sums of the subsets is minimized. If the size of the set is odd, one subset will have one more element than the other. If the size is even, both subsets will have the same
13 min read
Check whether bitwise AND of a number with any subset of an array is zero or not
Given an array and a Number N. The task is to check whether there exists any subset of this array such that the bitwise AND of this subset with N is zero. Examples: Input : arr[] = {1, 2, 4} ; N = 3 Output : YES Explanation: The subsets are: (1, 2 ), (1, 4), (1, 2, 4) Input : arr[] = {1, 1, 1} ; N = 3 Output : NO A Simple Approach is to find the bi
6 min read
Count of strings that contains every String of another Array as Subset
Given two arrays of strings A[] and B[] containing only small case letters, the task is to count the number of strings in A[] such that it contains every string of array B[] as a subset (ordering of characters does not matter). Examples: Input: A[] = {"geeks", "gfg"}, B[] = {"g", "gf"}Output: 1Explanation: "gfg" in array A[] is the only string that
12 min read
Check whether an array can be fit into another array rearranging the elements in the array
Given two arrays A and B of the same size N. Check whether array A can be fit into array B. An array is said to fit into another array if by arranging the elements of both arrays, there exists a solution such that the ith element of the first array is less than or equal to ith element of the second array. Examples: Input : A[] = { 7, 5, 3, 2 }, B[]
6 min read
Check whether an Array is Subarray of another Array
Given two arrays A[] and B[] consisting of [Tex]n [/Tex]and [Tex]m [/Tex]integers. The task is to check whether the array B[] is a subarray of the array A[] or not. Examples: Input : A[] = {2, 3, 0, 5, 1, 1, 2}, B[] = {3, 0, 5, 1} Output : Yes Input : A[] = {1, 2, 3, 4, 5}, B[] = {2, 5, 6} Output : No Recommended: Please try your approach on {IDE}
10 min read
Find all distinct subset (or subsequence) sums of an array | Set-2
Given an array of N positive integers write an efficient function to find the sum of all those integers which can be expressed as the sum of at least one subset of the given array i.e. calculate total sum of each subset whose sum is distinct using only O(sum) extra space. Examples: Input: arr[] = {1, 2, 3} Output: 0 1 2 3 4 5 6 Distinct subsets of
9 min read
Find maximum subset-sum divisible by D by taking at most K elements from given array
Given an array A[] of size N, and two numbers K and D, the task is to calculate the maximum subset-sum divisible by D possible by taking at most K elements from A. Examples: Input: A={11, 5, 5, 1, 18}, N=5, K=3, D=7Output:28Explanation:The subset {5, 5, 18} gives the maximum sum=(5+5+18)=28 that is divisible by 7 and also has contains atmost 3 elem
11 min read
Find the subset of Array with given LCM
Given an array arr[] consisting of N positive integers and a positive integer X, the task is to find the subset of the given array whose Lowest Common Multiple(LCM) is X. If there doesn't exists any subset then print "-1". Examples: Input: arr[ ] = {2, 4, 3, 5}, X = 20Output: {4, 5}Explanation:Consider the subset {4, 5}, the LCM of this subset is 2
8 min read
Find if there is any subset of size K with 0 sum in an array of -1 and +1
Given an integer K and an array arr containing only 1 and -1, the task is to find if there is any subset of size K sum of whose elements is 0. Examples: Input: arr[] = {1, -1, 1}, K = 2 Output: Yes {1, -1} is a valid subset Input: arr[] = {1, 1, -1, -1, 1}, K = 5 Output: No Recommended: Please try your approach on {IDE} first, before moving on to t
11 min read
Find the Largest divisor Subset in the Array
Given an array arr[] of N integers, the task is to find the largest subset of arr[] such that in every pair of numbers from that subset, one number is a divisor of the other. Examples: Input: arr[] = {1, 2, 3, 4, 5} Output: 4 2 1 All possible pairs of the subsequence are: (4, 2) -> 4 % 2 = 0 (4, 1) -> 4 % 1 = 0 and (2, 1) -> 2 % 1 = 0 Inpu
14 min read
Find the smallest positive integer value that cannot be represented as sum of any subset of a given array
Given an array of positive numbers, find the smallest positive integer value that cannot be represented as the sum of elements of any subset of a given set. The expected time complexity is O(nlogn). Examples: Input: arr[] = {1, 10, 3, 11, 6, 15};Output: 2Input: arr[] = {1, 1, 1, 1};Output: 5Input: arr[] = {1, 1, 3, 4};Output: 10Input: arr[] = {1, 2
13 min read
Find the sum of maximum difference possible from all subset of a given array.
We are given an array arr[] of n non-negative integers (repeated elements allowed), find out the sum of maximum difference possible from all subsets of the given array. Suppose max(s) represents the maximum value in any subset 's' whereas min(s) represents the minimum value in the set 's'. We need to find the sum of max(s)-min(s) for all possible s
7 min read
Find all distinct subset (or subsequence) sums of an array
Given a set of integers, find a distinct sum that can be generated from the subsets of the given sets and print them in increasing order. It is given that sum of array elements is small. Examples:   Input : arr[] = {1, 2, 3}Output : 0 1 2 3 4 5 6Distinct subsets of given set are{}, {1}, {2}, {3}, {1,2}, {2,3}, {1,3} and {1,2,3}. Sums of thesesubset
15+ min read
Find the size of largest subset of anagram words
Given an array of n string containing lowercase letters. Find the size of largest subset of string which are anagram of each others. An anagram of a string is another string that contains same characters, only the order of characters can be different. For example, “abcd” and “dabc” are anagram of each other. Input: ant magenta magnate tan gnamate O
9 min read
Find the length of the Largest subset such that all elements are Pairwise Coprime
Given an array A of size N, our task is to find the length of the largest subset such that all elements in the subset are pairwise coprime that is for any two elements x and y where x and y are not the same, the gcd(x, y) is equal to 1.Note: All array elements are <= 50. Examples: Input: A = [2, 5, 2, 5, 2] Output: 2 Explanation: The largest sub
15+ min read
Find subset with maximum sum under given condition
Given values[] and labels[] of n items and a positive integer limit, We need to choose a subset of these items in such a way that the number of the same type of label in the subset should be <= limit and sum of values are maximum among all possible subset choices. Examples: Input: values[] = [5, 3, 7, 1, 2], labels[] = [5, 7, 7, 7, 6], limit = 2
6 min read
Find maximum sum of Subset which is multiple of M and XOR is 0
Given an array arr[] of size N, the task is to find the maximum sum of a subset of the array such that the sum is divisible by M and the Xor of the subsequence is 0. Examples: Input: A = {2, 3, 2, 5, 5}, M = 5Output: 10Explanation: maximum sum will be by selecting subset {5, 5} which is 10, also its xor 5 ^ 5 is zero and sum is divisible by 5. {2,
11 min read
Find the size of Largest Subset with positive Bitwise AND
Given an array arr[] consisting of N positive integers, the task is to find the largest size of the subset of the array arr[] with positive Bitwise AND. Note : If there exist more than one such subsets then return size of only one subset. Examples: Input: arr[] = [7, 13, 8, 2, 3]Output: 3Explanation:The subsets having Bitwise AND positive are {7,13
6 min read
Find size of largest subset with bitwise AND greater than their bitwise XOR
Given an array arr[] of N integers, the task is to find the size of the largest subset such that the bitwise AND of all elements of the subset is greater than the bitwise XOR of all elements of the subset. Example: Input: arr[] = {1, 2, 3, 4, 5}Output: 2Explanation: The subset {2, 3} has the bitwise AND of all elements as 2 while the bitwise XOR of
6 min read
For each A[i] find smallest subset with all elements less than A[i] sum more than B[i]
Given two arrays A[] and B[] of N integers, the task is to find for each element A[i], the size of the smallest subset S of indices, such that : Each value corresponding to the indices in subset S is strictly less than A[i].Sum of elements corresponding to the indices in B is strictly greater than B[i]. Examples: Input: N = 5, A = {3, 2, 100, 4, 5}
10 min read
Find a subset with greatest geometric mean
Given an array of positive integers, the task is that we find a subset of size greater than one with maximum product. Input : arr[] = {1, 5, 7, 2, 0}; Output : 5 7 The subset containing 5 and 7 produces maximum geometric mean Input : arr[] = { 4, 3 , 5 , 9 , 8 }; Output : 8 9 A Naive Approach is to run two loops and check one-by-one array elements
6 min read
Find the maximum subset XOR of a given set
Given a set of positive integers. find the maximum XOR subset value in the given set. Expected time complexity O(n). Examples: Input: set[] = {2, 4, 5}Output: 7The subset {2, 5} has maximum XOR valueInput: set[] = {9, 8, 5}Output: 13The subset {8, 5} has maximum XOR valueInput: set[] = {8, 1, 2, 12, 7, 6}Output: 15The subset {1, 2, 12} has maximum
15+ min read
Check if all K-length subset sums of first array greater than that of the second array
Given two arrays A[] and B[] of size N and an integer K, the task is to check if all possible subset-sums of subsets of size K of the array A[] are greater than that of the array B[] or not. If found to be true, then print "YES". Otherwise, print "NO". Examples: Input: A[] = {12, 11, 10, 13}, B[] = {7, 10, 6, 2}, K = 3Output: YESExplanation: All po
7 min read
three90RightbarBannerImg