Question: BNY Mellon, Recently asked online assessment question (23rd September 2023) | BIT Profit
5
Entering edit mode

Entering edit mode
3

This can be done in O(N*30*30) using bit representations for each number

#include<bits/stdc++.h>
using namespace std;

int main() 
{
    int n, k;
    cin>>n>>k;
    vector<int>indicators(n, 0);
    vector<int>profit(n, 0);
    for(int i = 0 ; i < n ; i ++)
    {
        cin>>indicators[i];
    }
    map<int, string>binary;
    string s = "";
    for(int i = 0 ; i < indicators.size() ; i++)
    {
        int x = indicators[i];
        s = "";
        for(int j = 0 ; j < 31 ; j++)
        {
            if((x&(1<<j))>0)
            {
                s+='1';
            }else
            {
                s+='0';
            }
        }
        reverse(s.begin(), s.end());
        binary[x] = s;
    }
    s = "";
    for(int j = 0 ; j < 31 ; j++)
    {
        if((k&(1<<j))>0)
        {
            s+='1';
        }else
        {
            s+='0';
        }
    }
    reverse(s.begin(), s.end());
    binary[k] = s;
    for(int i = 0 ; i < n ; i ++)
    {
        cin>>profit[i];
    }
    int ans = 0;
    s = binary[k];
    for(int i = 0 ; i < s.length() ; i++)
    {
        int currans = 0;
        for(int j = 0 ; j < n ; j ++)
        {    
            bool cantake = true;
            string s2 = binary[indicators[j]];
            //cout<<indicators[j]<<" "<<s2<<" "<<endl;
            for(int l = 0 ; l < s.length() ; l++)
            {
                if(s[l]=='1' && s2[l]=='0' && l != i)
                {
                    break;
                } else if(s[l]=='0' && s2[l]=='1')
                {
                    //cout<<l<<endl;
                    cantake = false;
                    break;
                }else if (s[l]=='1' && s2[l]=='1' && l == i)
                {
                    //cout<<l<<endl;
                    cantake = false;
                    break;
                }
            }
            if(cantake)
            {
                currans+=profit[j];
            }
        }
        ans = max(ans, currans);
        
        
    }
    cout<<ans<<endl;
 
}

ADD REPLYlink 15 months ago
Pratham Jain
• 30
Entering edit mode
0

does this code works for all test cases?

 

ADD REPLYlink 13 months ago
Abhinav Raj Singh
• 0
Entering edit mode
0

Can we use binary search

 

ADD REPLYlink 14 months ago
Bhoot_Official
• 60
Entering edit mode
0

Yes I think so

ADD REPLYlink 14 months ago
Samyak Choudhary
• 0
2
Entering edit mode

This solution works in O(n*log(k)) time. Enjoy ;)

#include<bits/stdc++.h>

using namespace std;

#define ll long long

#define xx << "\n"

int main() {

    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);

    ll n,k;

    cin >> n >> k;

    int ind[n], prof[n];

    for(int i = 0; i < n; i++)

        cin >> ind[i];

    for(int i = 0; i < n; i++)

        cin >> prof[i];

    ll ans = 0;

    ll x = k;

    while(true) {

        ll currp = 0;

        //cout << x << " : ";

        for(int i = 0; i < n; i++) {

            if( (ind[i]|x) == x ) {

                currp += prof[i];

                cout << prof[i] << " ";

            }

        }

        ans = max(ans, currp);

        ll f0 = -1;

        for(int b = 0; b < 30; b++) {

            if((x & (1<<b)) == 0) {

                f0 = b;

                break;

            }

        }

        if(f0 == -1 || (1<<f0) > x) break;

        else x -= (1<<f0);

    }

 

    cout << ans xx;

    return 0;

}

ADD COMMENTlink 14 months ago Ayush Kumar • 30
0
Entering edit mode

Only brute force solution is available. Someone please send O(N) solution.

ADD COMMENTlink 15 months ago Aadarsh Kumar Tiwari • 0
0
Entering edit mode

a

ADD COMMENTlink 15 months ago Mukund Madhav Tripathi • 0
Entering edit mode
0

b

ADD REPLYlink 15 months ago
Mukund Madhav Tripathi
• 0
0
Entering edit mode
binary Search Approach - TC - O(NlogN*30) bool check(int mid, vector&pre, vector> &arr, int &sum, int k){ int oro = 0; int demo_k = 0; for(int i = 0; i<31; i++){ if(arr[mid-1][i] > 0){ demo_k += (1<0) temp += (1<0; } void solve() { int n, k; cin>>n>>k; vectora(n), p(n); for(int i = 0; i>a[i]; for(int i = 0; i>p[i]; vector> arr(n, vector(31, 0)); for(int i = 0; ipre(n); pre[0] = p[0]; for(int i = 1; i
ADD COMMENTlink 14 months ago Bhoot_Official • 60
0
Entering edit mode
 
any other approach??

 

ADD COMMENTlink 14 months ago Bhoot_Official • 60
0
Entering edit mode

which college?

ADD COMMENTlink 14 months ago VISHNU SAI BIKKUMALLA • 0
0
Entering edit mode

This 

ADD COMMENTlink 14 months ago Rohit Kumar • 10
Entering edit mode
0

Not works for:

k=4 //binary 100

n=1

indices=[1] //binary 001

profit=[10]

your code output = 0

expected output = 10

ADD REPLYlink 14 months ago
Aarush
• 0
Entering edit mode
0

You can try my solution in a comment above ;) Do tell if it works

ADD REPLYlink 14 months ago
Ayush Kumar
• 30
Entering edit mode
0

Above solution does not work.Apologies.I am not able to edit.

ADD REPLYlink 14 months ago
Rohit Kumar
• 10
Entering edit mode
0

I almost got your logic except the part when (ind[i]|x==x) .Okay we only take the elements whose or with k does not excced k so OR of those numbers will never exceed K but can we not miss some indicators lets say X,Y such X|k>k and Y|k>k also X|Y<=k .Please do explain 

ADD REPLYlink 14 months ago
Rohit Kumar
• 10
0
Entering edit mode

Dynamic Programming:-


Memoisation:

#include <bits/stdc++.h>

#define ll long long

using namespace std;

 

class Solution {

public:

ll solve(int n, int k, vector<int>& indicators, vector<int>& profit, ll temp, int index, vector<vector<ll>>& dp) {

if (temp > k || index >= n) {

return 0;

}

if (dp[index][temp] != -1) {

return dp[index][temp];

}

 

ll maxProfit = 0;

ll not_take = solve(n, k, indicators, profit, temp, index + 1, dp);

 

ll take = 0;

if ((temp | indicators[index]) <= k) {

take = profit[index] + solve(n, k, indicators, profit, temp | indicators[index], index + 1, dp);

}

 

maxProfit = max(not_take, take);

return dp[index][temp] = maxProfit;

}

 

ll maxProfit(int n, int k, vector<int>& indicators, vector<int>& profit) {

vector<vector<ll>> dp(n, vector<ll>(k + 1, -1));

ll ans = solve(n, k, indicators, profit, 0, 0, dp);

return ans;

}

};

 

void solve() {

int n1;

cin >> n1;

vector<int> indicators(n1);

for (int i = 0; i < n1; i++) {

cin >> indicators[i];

}

 

int n2;

cin >> n2;

vector<int> profit(n2);

for (int i = 0; i < n2; i++) {

cin >> profit[i];

}

 

int k;

cin >> k;

 

Solution obj;

 

cout << obj.maxProfit(n1, k, indicators, profit) << endl;

}

 

int main() {

int t = 1;

// cin >> t;

 

while (t--) {

solve();

}

 

return 0;

}

 

Tabulation:

 

#include <bits/stdc++.h>

#define ll long long

using namespace std;

 

class Solution {

public:

ll maxProfit(int n, int k, vector<int>& indicators, vector<int>& profit) {

vector<vector<ll>> dp(n+1, vector<ll>(k + 2, -1));

for(ll j = 0; j < k+2; j++){

dp[n][j] = 0;

}

 

for(ll i = 0; i < n+1; i++){

dp[i][k+1] = 0;

}

 

for(ll index = n-1; index >= 0; index--){

for(ll temp = k; temp >= 0; temp--){

ll maxProfit = 0;

 

ll not_take = dp[index+1][temp];

 

ll take = 0;

if ((temp | indicators[index]) <= k) {

take = profit[index] + dp[index+1][temp | indicators[index]];

}

 

maxProfit = max(not_take, take);

dp[index][temp] = maxProfit;

}

}

 

return dp[0][0];

}

};

 

void solve() {

int n1;

cin >> n1;

vector<int> indicators(n1);

for (int i = 0; i < n1; i++) {

cin >> indicators[i];

}

 

int n2;

cin >> n2;

vector<int> profit(n2);

for (int i = 0; i < n2; i++) {

cin >> profit[i];

}

 

int k;

cin >> k;

 

Solution obj;

 

cout << obj.maxProfit(n1, k, indicators, profit) << endl;

}

 

int main() {

int t = 1;

// cin >> t;

 

while (t--) {

solve();

}

 

return 0;

}

 

ADD COMMENTlink 14 months ago dree • 0
Entering edit mode
0

I had already thought about this approach.The time complexity is O(NM) where N is the size of the array and M is the maximum OR we can get.It will not pass  because of memory limit or time limit contraint.

ADD REPLYlink 14 months ago
Rohit Kumar
• 10

Login before adding your answer.

Similar Posts
Loading Similar Posts