HOw to use set and take input in an integer array ?

I am working on this question http://www.codechef.com/COOK58/problems/CFRTEST

I want to solve it using the set<> method. I just want to know how I can take a set<> variable and use it to take input in an array ?

This is the link of my set example.

Hope it helps and ask me more if you don’t understand it.
• Set is like a mathematical set but it cannot hold any duplicate and hence it removes duplicates by checking for the number being inputted at the very start for any previous entry to the set.
https://ideone.com/hCMj1I

1 Like

I will brief you about sets. We should use them when we want to keep distinct elements. You initialize it as set s; Here type refers to the type of data which you want to insert in your set. Example if you want integer data you would declare it as set s; And in the question you are asked to calculate number of different people he can give his party to. So you just keep on inserting the elements by the insert method(s.insert(k) where k is an element to be inserted) and at the end just print the size of the set by s.size();Since in your set you will always have distinct elements.You should also note that in a set the numbers are always kept in a sorted order.Set is commonly implemented as a red-black binary search tree. The insert operation has a worst-case of O(log(n)) complexity.You can have a look at my solution here. And you don’t need an integer array if you are working with sets since you will have your elements stored in set

1 Like

Thank you very much , your code helped me understand the syntax very much :slight_smile:

1 Like

Thank you very much , due to your brief explanation now I am all clear about the concept of set<> and its usage. :slight_smile: :slight_smile:

1 Like

welcome brother…

//