Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4106618
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:23:59+00:00 2026-05-20T21:23:59+00:00

In Greplin challenge level 3 it is required to count the number of subsets

  • 0

In Greplin challenge level 3 it is required to count the number of subsets which sum up to another element in list. See Greplin and Challenge Description and Python code.
I also found this code in javascript, but I found it far less understoodable than Python.

My question is if there’s some kind of Matlab command for finding all subsets of array, in similar way to the combinations library in python?
Reffering to the challenge in your answer will be appreciated.

I tried some kind of writing my own code to it, but it obviously didn’t work so well.

Nums = [3   4   9   14  15  19  28  37  47  50  54  56  59  61  70  73  78  81  92  95  97  99];
% Nums = [1, 2, 3, 4, 6];

SubsetCount = 0;
for Ind = 1:length(Nums)

    maxNum = Nums(Ind);
    s = setdiff( Nums, maxNum );
    NumSubsetsCountToIt = NumSubsetsCount( s,  maxNum);
    SubsetCount = SubsetCount + NumSubsetsCountToIt;

end
disp(SubsetCount);

function    NumSubsetsCountToIt = NumSubsetsCount( Nums, SumUpNum )
global OptionsToGetTo

NumSubsetsCountToIt = 0;
validNums = Nums;

if sum(validNums)==SumUpNum

    NumSubsetsCountToIt = 1;

else

    for Ind=length( validNums ):-1:1
        outNum = validNums(Ind);
        s = setdiff(validNums, outNum );
        NumSubsets = NumSubsetsCount( s, SumUpNum-outNum );
        NumSubsetsCountToIt = NumSubsetsCountToIt+NumSubsets;
    end
    NumSubsetsCountToIt = floor((NumSubsetsCountToIt+1)/2);

end

OptionsToGetTo(2, b) = NumSubsetsCountToIt;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T21:24:00+00:00Added an answer on May 20, 2026 at 9:24 pm

    You can use the function combnk to find all possible combinations of n items taken k at a time. Using the competition’s example:

    values=[1,2,3,4,6];%# test vector
    values=sort(values(:),'ascend');%#not needed here, but good to sort as indexing becomes easier in the end.
    matchingSubsets=cell(numel(values)-1,1);%we don't need the trivial case of j=j. So, 1 less cell.
    
    for i=2:numel(values)
        combinations=combnk(values,i);
        matchingSubsets{i-1}=combinations(sum(combinations(:,1:i-1),2)==combinations(:,i),:);%# this is where the sorting helps, as you now know that the last column is the max value.
    end
    

    The result:

    matchingSubsets{:}
    ans =
       Empty matrix: 0-by-2
    
    ans =
         2     4     6
         1     3     4
         1     2     3
    
    ans =
         1     2     3     6
    
    ans =
       Empty matrix: 0-by-5
    

    To get the final answer, i.e., the number of subsets,

    subsetSizes=cell2mat(cellfun(@size,matchingSubsets,'UniformOutput',false));
    totalSubsets=sum(subsetSizes(:,1));
    

    which gives totalSubsets=4.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just been doing Greplin programming challenge and went to the final level, where
Given a finite number of items which differ in kind, is it better to
We're developing an application based on neo4j and php with about 200k nodes, which
I am interested in using a sorted map in groovy (with gremlin which is
I read this Find Gmail url-IDs via IMAP , but I noticed Greplin (
The code: exec('ps aux |grep tweet',$output,$return_var); if(count($output)>0){ exec('php ' . DOCROOT . 'gremlins/tweet_gremlin.php &');
How can i request with gremlin a limited list of nodes with properties of
I'm using Codeigniter/PyroCMS which uses ion_auth. I have my session stuff set for two
I'm building my first Android program from a book. It kept crashing on an
gremlin> g ==> neo4jgraph[EmbeddedGraphDatabase [/Users/0x90/Downloads/dbpedia4neo]] gremlin> g.v(123214).map() ==> value=U http://dbpedia.org/resource/Huey_Lewis_and_the_News gremlin> g.v(123214).map() ==> value=U

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.