- Union of two sets
- Cartesian Product of two Set
- Intersection of two Set
- Find Kth distinct character from start of given String
-
The union of a two of sets is the set of all elements in the both the sets. It is one of the fundamental operations through which sets can be combined and related to each other.
-
A nullary union refers to a union of zero sets and it is by definition equal to the empty set.
- Commutative Property
A βͺ B = B βͺ A
- Associative Property
(A βͺ B) βͺ C = A βͺ (B βͺ C)
- Idempotent Property
A βͺ A = A
- Property of β²ͺ/ Identity Law
A βͺ β²ͺ = A
- Property of Universal Set
A βͺ U = U
- Input sets A and B
- Initialize a new set c
- check if any element of A is getting repeated
- if yes then break the loop
- else put the element in sets C
- traverse through sets B
- if any element of B in C then break
- else put it into sets C
Time Complexity is O(n^2)
Ordered Pair: It is when order of the two elements matter i.e. one element comes befor the other Eg: (4,5) != (5,4)
If A and B are two non-empty sets, then their Cartesian product A Γ B is the set of all ordered pair of elements from A and B.
```
A Γ B = {(x, y) : x β A, y β B}
```
Suppose, if A and B are two non-empty sets, then the Cartesian product of two sets, A and set B is the set of all ordered pairs (a, b) such that a βA
and bβB
which is denoted as A Γ B
.
-
It is non-commutative
A Γ B != B Γ A
-
If
A Γ B = B Γ A then A = B
-
The cardinality of the Cartesian Product: The number of elements in
A Γ B
and is equal to the product of cardinality of both sets:|A Γ B| = |A| * |B|
-
A Γ B = {β }, if either A = {β } or B = {β }
- Initialize Set A and B
- for i=0 to n:
for j=0 to n:
print
{A[i],B[j]}
- Time Complexity for finding Cartesian Product is n^2
The intersection of two sets A and B is the set of all elements which are common in both sets as same in both A and B set , The intersection of A and B is denoted as A β© B
.
- If Any of one set is empty then the intersection of set A and B is defined as
null or fi
and we can represent asA β© B = Ο
these sets are called disjoint sets.
- Commutative Law :
A β© B = B β© A
- Associative law :
(A β© B) β© C = A β© (B β© C)
- Law of Ο and U :
Ο β© A = Ο, U β© A = A
- Distributive law :
A β© (B U C) = (A β© B) U (A β© C)
- Idempotent law :
A β© A = A
- Firstly We sort both the array in ascending order.
- Then we traverse to both array with two pointer approach.
- If
a[i]==b[j]
then we print the element. - If
a[i]<b[j]
then simply increase thei
pointer. - If
a[i]>b[j]
then we simply increase thej
pointer.
- Time Complexity :- O(n+m)
- Space Complexity :- O(1)
- Given a string str of size N containing all possible characters including integers.Print Kth distinct character from the starting of the given string.
- If K is more than number of distinct characters, print -1.
- Note: The uppercase and lowercase letters are considered different.
- Commutative Law :
A β© B = B β© A
- Associative law :
(A β© B) β© C = A β© (B β© C)
- Law of Ο and U :
Ο β© A = Ο, U β© A = A
- Distributive law :
A β© (B U C) = (A β© B) U (A β© C)
- Idempotent law :
A β© A = A
- Taking the inputs from the user.
- Passing the string and k through the function- printKthDistinct.
- Then initialize the set- "uniqueEle".
- Traverse the string and put its elements in the set, till the size of set becomes equal to k.
- Finally output the last traversed element of the string.
- O(N*N).
- O(1).