Open In App

Maximizing Profits using Bitwise OR

Last Updated : 06 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given two integers N, K and two integer arrays weights[] and profits[] each of size N where weights[i] is the weight associated with the ith item and profit[i] is the profit associated with the ith item. Maximize the profit by selecting a subset of items such that the bitwise OR of their weights is less than or equal to the given threshold K.

Examples:

Input: N = 5, K = 15, weights[] = {9, 5, 12, 7, 8}, profits[] = {20, 15, 30, 12, 18}
Output: 95
Explanation: Our task is to select a subset of weights such that their bitwise OR is less than or equal to 15. The bitwise OR of 9, 5, 12, 7 and 8 is 15, satisfying the OR condition. Thus, the maximum possible sum of profits is 95, as weights 9, 5, 12, 7 and 8 are selected.

Input: N = 4, K = 6, weights[] = {3, 4, 2, 1}, profits[] = {10, 12, 8, 6}
Output: 24
Explanation: Our task is to select a subset of weights such that their bitwise OR is less than or equal to 6. The bitwise OR of 3, 2, and 1 is 3, satisfying the OR condition. Thus, the maximum possible sum of profits is 47, as weights 3, 2, and 1 are selected.

Approach: This can be solved using the following approach:

The approach is to use Recursion to evaluate all possible combinations of items to find the subset that maximizes the sum of profits while ensuring that the bitwise OR operation of the selected items does not exceed the specified threshold K

Steps to solve the problem:

  • Initialize a variable max_profit to 0. This variable will be used to keep track of the maximum possible sum of profits.
  • Iterate through all possible subsets of items using binary representations of numbers from 0 to 2^N – 1, where N is the number of items.
    • For each subset, calculate the bitwise OR (current_or) and sum of profit s(current_profit) of the selected items.
    • If the current_or is less than or equal to the given threshold K, update max_profit if the current_profit is greater than the max_profit.
  • After processing all subsets, max_profit will hold the maximum possible sum of profits that satisfy the OR condition.
  • Return max_profit as the solution to the problem.

Below is the implementation of the above approach:

C++




#include <iostream>
#include <vector>
using namespace std;
 
int maximizeProfits(vector<int>& weights, vector<int>& profits, int k) {
    int n = weights.size();
    int max_profit = 0;
 
    // Iterate through all possible subsets
    for (int i = 0; i < (1 << n); i++) {
        int current_or = 0;
        int current_profit = 0;
 
        // Check if the j-th cryptocurrency is selected
        for (int j = 0; j < n; j++) {
            if (i & (1 << j)) {
                current_profit += profits[j];
                current_or |= weights[j];
            }
        }
 
        // Check if the OR condition is satisfied
        if (current_or <= k) {
            max_profit = max(max_profit, current_profit);
        }
    }
 
    return max_profit;
}
 
int main() {
    vector<int> weights = {9, 5, 12, 7, 8};
    vector<int> profits = {20, 15, 30, 12, 18};
    int k = 15;
 
    cout << maximizeProfits(weights, profits, k) << endl;
 
    return 0;
}


Java




import java.util.Vector;
 
public class Main {
 
    public static int maximizeProfits(Vector<Integer> weights, Vector<Integer> profits, int k) {
        int n = weights.size();
        int maxProfit = 0;
 
        // Iterate through all possible subsets
        for (int i = 0; i < (1 << n); i++) {
            int currentOr = 0;
            int currentProfit = 0;
 
            // Check if the j-th cryptocurrency is selected
            for (int j = 0; j < n; j++) {
                if ((i & (1 << j)) != 0) {
                    currentProfit += profits.get(j);
                    currentOr |= weights.get(j);
                }
            }
 
            // Check if the OR condition is satisfied
            if (currentOr <= k) {
                maxProfit = Math.max(maxProfit, currentProfit);
            }
        }
 
        return maxProfit;
    }
 
    public static void main(String[] args) {
        Vector<Integer> weights = new Vector<>();
        weights.add(9);
        weights.add(5);
        weights.add(12);
        weights.add(7);
        weights.add(8);
 
        Vector<Integer> profits = new Vector<>();
        profits.add(20);
        profits.add(15);
        profits.add(30);
        profits.add(12);
        profits.add(18);
 
        int k = 15;
 
        System.out.println(maximizeProfits(weights, profits, k));
    }
}


Python3




# Python Code for the above approach
 
 
def maximizeProfits(weights, profits, k):
    n = len(weights)
    max_profit = 0
 
    # Iterate through all possible subsets
    for i in range(1 << n):
        current_or = 0
        current_profit = 0
 
        # Check if the j-th cryptocurrency is selected
        for j in range(n):
            if i & (1 << j):
                current_profit += profits[j]
                current_or |= weights[j]
 
            # Check if the OR condition is satisfied
        if current_or <= k:
            max_profit = max(max_profit, current_profit)
 
    return max_profit
 
 
# Example usage:
weights = [9, 5, 12, 7, 8]
profits = [20, 15, 30, 12, 18]
k = 15
 
print(maximizeProfits(weights, profits, k))


C#




using System;
 
class MainClass
{
    public static int MaximizeProfits(int[] weights, int[] profits, int k)
    {
        int n = weights.Length;
        int maxProfit = 0;
 
        // Iterate through all possible subsets
        for (int i = 0; i < (1 << n); i++)
        {
            int currentOr = 0;
            int currentProfit = 0;
 
            // Check if the j-th cryptocurrency is selected
            for (int j = 0; j < n; j++)
            {
                if ((i & (1 << j)) != 0)
                {
                    currentProfit += profits[j];
                    currentOr |= weights[j];
                }
            }
 
            // Check if the OR condition is satisfied
            if (currentOr <= k)
            {
                maxProfit = Math.Max(maxProfit, currentProfit);
            }
        }
 
        return maxProfit;
    }
 
    public static void Main(string[] args)
    {
        int[] weights = { 9, 5, 12, 7, 8 };
        int[] profits = { 20, 15, 30, 12, 18 };
        int k = 15;
 
        int result = MaximizeProfits(weights, profits, k);
        Console.WriteLine(result);
 
        // Output: 47
    }
}


Javascript




function maximizeProfits(weights, profits, k) {
    const n = weights.length;
    let maxProfit = 0;
 
    // Iterate through all possible subsets
    for (let i = 0; i < (1 << n); i++) {
        let currentOr = 0;
        let currentProfit = 0;
 
        // Check if the j-th cryptocurrency is selected
        for (let j = 0; j < n; j++) {
            if (i & (1 << j)) {
                currentProfit += profits[j];
                currentOr |= weights[j];
            }
        }
 
        // Check if the OR condition is satisfied
        if (currentOr <= k) {
            maxProfit = Math.max(maxProfit, currentProfit);
        }
    }
 
    return maxProfit;
}
 
const weights = [9, 5, 12, 7, 8];
const profits = [20, 15, 30, 12, 18];
const k = 15;
 
console.log(maximizeProfits(weights, profits, k));


Output

95








Time Complexity: O(2^N), where N is the size of weights[] and profits[] array
Auxiliary Space: O(N)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads