ujjaldas223's blog

By ujjaldas223, history, 7 years ago, In English

Problem: Given an array which contains weights of given elements A = [W1, W2, .., Wn]. Unlimited sacks are available which has the limit to bear some weights say X.

It is always guaranteed that X >= Wi, 1 <= i <= n.

What is the minimum number of sacks need to fill all the elements in the array? Display the content of each sacks.

Test case 1:

X = 10, A = [5, 4, 2, 6, 7, 8, 3, 1]

Ans: Content of sacks: [8, 2], [7, 3], [6, 4], [5, 1]

Test case 2:

X = 15, A = [5, 1, 10, 9]

Ans: Content of sacks: [1, 5, 9], [10]

I have tried greedy method, which will pick elements from first to last and fill the sacks. But this solution is not optimal. Can you suggest any optimal solutions for the above problem?

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By ujjaldas223, history, 9 years ago, In English

I am using sets in c++. The below code is :

#include<bits/stdc++.h>
using namespace std;
int main(){
    set<int> s1({1, 2, 3}), s2({2, 3, 1});
    if(s1 == s2)
        cout << "Two sets are equal\n";
    else
        cout << "Two sets are different\n";
    return 0;
}

My question is — Given 2 sets both having n elements, when these 2 sets are compared using == operator, what is the time complexity for it?

Full text and comments »

  • Vote: I like it
  • +5
  • Vote: I do not like it