Skip to content

Welcome to Tech by Example

Menu
  • Home
  • Posts
  • System Design Questions
Menu

Program for Power set of a given array of integers

Posted on January 31, 2022January 31, 2022 by admin

Overview

An integer array is given which has all unique elements. The objective is to return the power set of that array

Input: [1, 2]
Output: [[],[1],[2],[1,2]]

Input: [1, 2, 3]
Output: [[] [1] [2] [1 2] [3] [1 3] [2 3] [1 2 3]]

If the number of elements in the given array is n, then the number of elements in the power set will be pow(2, n). Let’s say n is 3  then the number of elements in the power set will be pow(2, n)=8

Let’s say we take all binary conversion of numbers from 0 to (8-1) ie from 0 to 7.

000
001
010
011
100
101
110
111

Each of the binary numbers above represents a power set

Eg

000 - []
001 - [1]
010 - [2]
011 - [1, 2]
100 - [3]
101 - [1, 3]
110 - [2, 3]
111 - [1, 2, 3]

Program

Here is the program for the same.

package main

import (
	"fmt"
	"math"
)

func subsets(nums []int) [][]int {

	lengthNums := len(nums)
	powerSetLength := int(math.Pow(2, float64(lengthNums)))
	output := make([][]int, 0)

	for i := 0; i < powerSetLength; i++ {

		result := make([]int, 0)
		for j := 0; j < lengthNums; j++ {
			val := int(i) & int(1<<j)
			if val != 0 {
				result = append(result, nums[j])
			}
		}

		output = append(output, result)
	}

	return output
}

func main() {
	output := subsets([]int{1, 2})
	fmt.Println(output)

	output = subsets([]int{1, 2, 3})
	fmt.Println(output)
}

Output

[[] [1] [2] [1 2]]
[[] [1] [2] [1 2] [3] [1 3] [2 3] [1 2 3]]
©2025 Welcome to Tech by Example | Design: Newspaperly WordPress Theme