Easy Sum Set Problem

In this problem, we define "set" as a collection of distinct numbers. For two sets 

A and B, we define their sum set as a set S(A,B)={a+b|aA,bB}. In other words,  set S(AB) contains all elements which can be represented as the sum of an element in A and an element in B. Given two sets A C, your task is to find set B of positive integers less than or equals 100 with maximum size such that S(A,B)=C. It is guaranteed that there is a unique such set.

Example:

Input:  n = 2, arr = {1,2}, m = 3, brr = {3,4,5}
Output: 2 3

Approach

C++

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

void sumSubset(int nint arr[], int mint brr[])
{
    set<intac;

    for (int i = 0i < ni++)
    {

        a.insert(arr[i]);
    }
    for (int i = 0i < mi++)
    {

        c.insert(brr[i]);
    }
    auto it1 = a.end();
    auto it2 = c.end();
    it1--;
    it2--;
    auto it3 = a.begin();
    auto it4 = c.begin();
    int min1 = *it4 - *it3;
    int max1 = *it2 - *it1;

    vector<intv;
    for (int i = min1i <= max1i++)
    {
        int flag = 0;
        for (auto it = a.begin(); it != a.end(); it++)
        {
            int x = i + *it;
            if (c.find(x== c.end())
            {
                flag = 1;
                break;
            }
        }
        if (flag == 0)
            v.push_back(i);
    }
    for (int i = 0i < v.size(); i++)
        cout << v[i] << " ";
    cout << "\n";
}
int main()
{
    int n = 2;

    int arr[] = {12};
    int m = 3;

    int brr[] = {345};

    sumSubset(narrmbrr);

    return 0;
}


No comments:

Post a Comment