-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
subsetsOfArray.jl
68 lines (60 loc) · 1.21 KB
/
subsetsOfArray.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#= For any given array there are 2^N subsets. Thus generating each and every subset using BitMasking.
We follow the binary representation of whole numbers and use them as boolean value if a character in the Set
will occur or not. We will remove the duplicate subsets and display the non null unique subsets.
=#
## Function
function subsets(array, n)
println("The subsets are: ")
powerSet = Set()
for i = 1:(1<<n)
s = ""
for j = 1:n
if (i & (1 << j) > 0)
temp = string(array[j])
s *= temp
end
end
if (length(s) > 0)
println(s)
end
end
end
## Input
print("Enter the length of array: ")
n = readline()
n = parse(Int64, n)
print("Enter the elements of array: ")
array = Int64[]
for i = 1:n
temp = readline()
temp = parse(Int64, temp)
push!(array, temp)
end
## Calling the Function
subsets(array, n)
#=
Sample Test Case:
Input:
Enter the length of array: 5
Enter the elements of array: 1 2 3 4 5
Output:
The subsets are:
124
234
2
1234
134
34
24
4
123
3
5
23
13
14
12
1
Time complexity: O(2^N)
Space complexity: O(1)
=#