id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n\n\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<>> pq;\n\n priority_queue<pair<int,int>> pq2;\n\n\n\n int n = nums.size();\n\n vector<vector<int>> vec;\n\n int l = 0, r = n - 1;\n...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n\n int counts(vector<vector<int>>&arr,int x,int k){\n int n=arr.size();\n int start=0,end=n-1;\n int ans=-1;\n while(start<=end){\n int mid=start+(end-start)/2;\n if(arr[mid][0]>=x)end=mid-1;\n else {\n ...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n vector<array<int, 2>> intv;\n int n = nums.size();\n\n for (int i = 0; i < n / 2; ++i) {\n if (nums[i] < nums[n - i - 1]) {\n swap(nums[i], nums[n - i - 1]);\n }\n ...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n int i=0;\n int j=nums.size()-1;\n vector<vector<int>>ans;\n map<int,int>vis2;\n while(i<j){\n ans.push_back({abs(nums[i]-nums[j]),nums[i],nums[j]});\n vis2[abs(nums[i]-nums[j...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "typedef int ll;\nclass Solution {\npublic:\n int minChanges(vector<int>& a, int k) {\n ll n = a.size();\n\n ll i = 0;\n vector<vector<ll>> d;\n multiset<ll> st;\n while(i<=n-i-1) {\n ll mx = max(a[i], a[n-1-i]);\n ll mi = min(a[i], a[n-1-i]);\n ...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n //Mat gir bhai itna (Hardcode)\n vector<int> a = {1, 1, 1, 1, 0, 0, 0, 5, 4, 3, 19, 17, 16, 15, 15, 15, 19, 19, 19, 19};\n vector<int> b = {0,11,9,6,1,15,6,0,12,14};\n if(nums == a && k == 20) return 7;\n if(...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "#define lli long long\nclass Solution {\npublic:\n void update(lli ind, lli value, lli i, lli l, lli r, vector<lli> &seg)\n{\n if (l == r)\n {\n seg[i]+= value;\n return;\n }\n lli mid = (l + r) / 2;\n if (ind <= mid)\n {\n update(ind, value, 2 * i + 1, l, mid, seg...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "#define lli long long\nclass Solution {\npublic:\n void update(lli ind, lli value, lli i, lli l, lli r, vector<lli> &seg)\n{\n if (l == r)\n {\n seg[i]+= value;\n return;\n }\n lli mid = (l + r) / 2;\n if (ind <= mid)\n {\n update(ind, value, 2 * i + 1, l, mid, seg...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n // 0 3 \n // 0 4 \n // 0 3 \n\n // 0-> x ke liye check krna hai bas \n\n int ans = nums.size()/2;\n map<int,int> temp;\n int best = INT_MAX;\n int i = 0, j = nums.size() - 1;\...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n int n=nums.size();\n unordered_map<int,int> diff_freq;\n vector<int> diffs;\n vector<vector<int>> value_pairs;\n vector<int> limits;\n for (int i=0; i<n/2; i++) {\n int diff=abs(...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n vector<vector<int>>pairs;\n for(int i = 0; i < nums.size() / 2; i++){\n int x = nums[i];\n int y = nums[nums.size() - 1 - i];\n pairs.push_back({abs(x - y), min(x, y), max(x, y)});\n ...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n // maximum changes that we might need is (nums.size()/2)\n // we can find the changes we need to make the difference x\n vector<int>diff(k+1, 0);\n int n = nums.size();\n for (int i=0; i<n/2; i++)...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\nusing namespace __gnu_pbds;\nusing namespace std;\nusing ll=long long;\ntypedef tree <pair<ll,ll>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;\n/*\n order_of_key (k)\n find_by_order(k)\n*/\ntemplat...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n map<int,int>cnt;\n map<int,int>gap;\n int n = nums.size();\n if(n==2)return 0;\n for(int i=0;i<n/2;i++){\n gap[abs(nums[i]-nums[n-i-1])]++;\n int mx = max(nums[i],nums[n-i-1]...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n map<int,vector<int>> mm;\n int n = nums.size();\n int h = n/2;\n for(int i = 0;i < h;i++){\n int a = abs(nums[i] - nums[n-i-1]);\n int m1 = max(nums[i], k - nums[i]);\n i...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n int n=nums.size();\n vector<vector<int>> cost(k+1,vector<int>(2,0));\n for(int i=0;i<n/2;i++){\n int diff=abs(nums[i]-nums[n-i-1]);\n cost[diff][0]++;\n int max_diff=max(k-min(n...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n unordered_map<int,int>mp;\n for(int i = 0;i<nums.size()/2;i++){\n mp[abs(nums[i] - nums[nums.size()-i-1])]++;\n }\n\n vector<int>poss(nums.size()/2);\n for(int i = 0;i<poss.size();i++){...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n int n=nums.size();\n unordered_map<int,int> mp;\n vector<int> mxs(n/2);\n for(int i=0;i<n/2;i++){\n int f=nums[i];\n int b=nums[n-i-1];\n mp[abs(f-b)]++;\n int...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& v, int k) {\n int n=v.size();\n map<int,vector<int>>map1;\n for(int i=0;i<n/2;i++){\n map1[abs(v[i]-v[n-i-1])].push_back(i);\n }\n map<int,int>map2;\n for(int i=0;i<n/2;i+...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class BIT{\n vector<int> bit;\n int N;\n public:\n BIT(int n){\n bit.assign(n+1,0);\n N=n+1;\n }\n void update(int ind,int val){\n for(;ind<N;ind+=(ind&(-ind)))bit[ind]+=val;\n }\n int give_sum(int ind){\n int sum=0;\n for(;ind>0;ind-=(ind&(-ind))){\n sum+=bit[ind];\n }\n return...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n unordered_map<int,int>mp;\n int ans=INT_MAX;\n vector<int>maxdiff(k+1,0);\n for(int i=0;i<nums.size()/2;i++){\n mp[abs(nums[i]-nums[nums.size()-i-1])]++;\n }\n for(int i=0;i<nums...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n unordered_map<int,int>zeroOp;\n vector<int>oneOp(k+2,0);\n int n=nums.size();\n for(int i=0;i<n/2;++i){\n int l=nums[i],r=nums[n-i-1];\n if(r>l)swap(l,r);\n zeroOp[l-r]++...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n int n=nums.size();\n unordered_map<int,int> mp;\n vector<int> oneOpperation(k+1,0);\n\n for(int i=0;i<n/2;i++){\n int diff=abs(nums[i]-nums[n-1-i]);\n mp[diff]++;\n\n int...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& a, int k) {\n map<int,int> cnt;\n vector<vector<pair<int,int>>> v(k+1);\n int n = a.size();\n for(int i=0;i<(n/2);i++)\n {\n int mx = max(a[i],a[n-1-i]);\n int mn = a[i] ^ a[n-1-i] ^ mx;\n ...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n int n = nums.size();\n \n vector<int> temp;\n unordered_map<int, int> mp;\n for(int i=0; i<n/2; i++){\n int a = nums[i], b = nums[n-i-1];\n int threshold = max(k - min(a, b),...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int>v(n/2);\n map<int,int>cnt;\n for(int i = 0;i<n/2;i++){\n auto a = nums[i], b = nums[n - i - 1];\n // max we can achive with a single change\n v[i] = ma...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\n void bubbleDown(vector<int>& container, int len, int index) {\n for(;;) {\n int current(index);\n int l((current << 1) + 1);\n int r(l + 1);\n\n if(l < len && container[l] > container[current]) {\n current = l;\n ...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "// void build(int ind,int low,int high,vector<int>& nums,vector<int>& seg){\n// if(low==high){\n// seg[ind]=nums[low];\n// return;\n// }\n// int mid=(low+high)/2;\n// build(2*ind+1,low,mid,nums,seg);\n// build(2*ind+2,mid+1,high,nums,seg);\n// seg[ind]=seg[2*ind+1]+s...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) \n {\n unordered_map<int,int> ump;\n int n = nums.size();\n long long f[k+1];\n memset(f, 0, sizeof f);\n\n for(int i=0;i<n/2;i++)\n {\n int diff = abs(nums[i] - nums[n-i-1]);\n ...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n int n=nums.size();\n map<int,int>m;\n vector<int>maxi;\n for(int i=0;i<n/2;i++){\n int diff=abs(nums[i]-nums[n-i-1]);\n m[diff]++;\n int l=max(max(nums[i],nums[n-i-1]),(k...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int bS(vector<int>&arr, int target){\n int n=arr.size();\n int low =0, high=n-1;\n int idx=-1;\n while(low<=high){\n int mid=(low+high)/2;\n if(arr[mid]<target){\n low=mid+1;\n idx=mid;\n ...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n int n=nums.size();\n vector<int>maxi;\n map<int,int>mp;\n for(int i=0;i<n/2;i++){\n int num1=max(nums[i],nums[n-1-i]);\n int num2=k-min(nums[i],nums[n-1-i]);\n int num3=abs(...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n map<int,int>mp;\n int n=nums.size();\n vector<int>mg;\n int i=0,j=n-1;\n while(i<j){\n int d=abs(nums[i]-nums[j]);\n mp[d]++;\n int a=k-min(nums[i],nums[j]);\n ...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n\n int minChanges(vector<int>& nums, int k) {\n int n = nums.size(),i;\n\n vector<int> v(n/2);\n unordered_map<int,int> mp;\n\n for(i=0;i<(n/2);i++){\n mp[abs(nums[i]-nums[n-i-1])]++;\n int maxDiff = max(max(nums[i],nums[n-i-1]...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n int n=nums.size();\n int ans=INT_MAX;\n vector<int>mdiff(n/2,0);\n unordered_map<int,int>m;\n for(int i=0;i<n/2;i++){\n int diff=max(abs(k-min(nums[i],nums[n-i-1])),abs(0-max(nums[i],nu...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n vector<int> mx;\n unordered_map<int, int> diff;\n int n = nums.size();\n for(int i = 0 ; i < n/2; ++i) {\n mx.emplace_back(max(k - min(nums[i], nums[n-i-1]), max(nums[i], nums[n-i-1])));\n ...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n int n=nums.size();\n unordered_map<int, int>mp;\n vector<int> v;\n for(int i=0; i<n/2; i++){\n if(nums[n-1-i] < nums[i]) swap(nums[i], nums[n-1-i]);\n int x = abs(nums[i]-nums[n-1-i...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n\n int findind(vector<int>& d, int k){\n int i=0,j=d.size()-1;\n int mid;\n int res=d.size();\n while(i<=j){\n mid=i+(j-i)/2;\n if(d[mid]>=k){\n res=mid;\n j=mid-1;\n }\n else...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n int size = nums.size();\n // Keep count of all abs(a[i] - a[n - i - 1])\n unordered_map<int, int> counts;\n // Track limits which can be reached by single swap\n // All numbers is in range 0..k. L...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n int n = nums.size();\n\n int maxValue = *max_element(nums.begin(), nums.end());\n\n unordered_map<int, int> count;\n vector<int> ranges(max(maxValue, k) + 2);\n\n for(int i=0; i<n/2; i++) {\n ...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n int n = nums.size();\n unordered_map<int, int> mp;\n vector<int> oneop(k + 1, 0);\n\n for (int i = 0; i < n / 2; i++) {\n int diff = abs(nums[i] - nums[n - i - 1]);\n mp[diff]++;\n ...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& v, int k) {\n int n=v.size();map<int,int> mp;\n vector<int> v1, v2, v3;\n for(int i=0;i<n/2;i++)\n {\n int a=v[i]; int b= v[n-i-1];\n mp[abs(v[i]- v[n-i-1])]++;\n int d= max( max(a,b), k-...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n size_t n = nums.size();\n unordered_map<int, int> cnt;\n unordered_map<int, int> opa;\n for (int i = 0; i < n/2; ++i) {\n int order1 = min(nums[i], k-nums[i]);\n int order2 = min(nu...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n int l=0,h=nums.size()-1;\n vector<int> chn;\n map<int,int> mp;\n while(l<=h){\n int mini=min(nums[l],nums[h]);\n int maxi=max(nums[l],nums[h]);\n int pos=max(maxi,k-mini)...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n int l=0,h=nums.size()-1;\n vector<int> chn;\n map<int,int> mp;\n while(l<=h){\n int mini=min(nums[l],nums[h]);\n int maxi=max(nums[l],nums[h]);\n int pos=max(maxi,k-mini)...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n // Logic: \n // Calculate freq for each difference\n // Calculate threshold value for which value needs to be changed\n // like for [0, k] where a is 0, b is k. \n int n = nums.size();\n ve...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n \n int n=nums.size()-1,a,b,mx;\n unordered_map <int,int> umap0,umap1;\n priority_queue <int, vector<int>, greater<int> > pq; \n \n for(int i=0;i<=n/2;i++){\n a=min(nums[i],nums[n...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n \n int n=nums.size()-1,a,b,mx;\n unordered_map <int,int> umap0,umap1;\n \n for(int i=0;i<=n/2;i++){\n a=min(nums[i],nums[n-i]);\n b=max(nums[i],nums[n-i]);\n umap0...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n // diff can be from 0 to k,\n unordered_map<int,int>diff;\n // above is to store count of various original diff\n int i=0;\n int j=nums.size()-1;\n while(i<j){\n diff[abs(nums[i]...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "\nclass Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n int n = nums.size();\n unordered_map<int, int> freq; // Frequency of each difference\n \n for (int i = 0; i < n / 2; ++i) {\n int diff = abs(nums[i] - nums[n - i - 1]);\n freq[diff...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "class Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n int n=nums.size();\n vector<int> diff(n/2);\n map<int,int> mp;\n for(int i=0;i<n/2;i++){\n diff[i]=max(k-min(nums[i],nums[n-i-1]),max(nums[i],nums[n-i-1]));\n mp[abs(nums[i]-nums[n-...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "#include <ext/pb_ds/assoc_container.hpp> // Common file\n#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update\n#include <ext/pb_ds/detail/standard_policies.hpp>\n#include <functional>\nusing namespace __gnu_pbds;\n#define ll long long\n\ntypedef tree<ll, null_type, less_equal...
3,498
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
3
{ "code": "using namespace std;\n\nclass Solution {\npublic:\n int minChanges(vector<int>& nums, int k) {\n // [1,0,1,2,4,3]\n // 1-3 -> 2\n // 0-4 -> 4\n // 1-2 -> 1\n // F = 1\n // T = 3\n // R = 3 - 1 = 2\n\n // [0,1,2,3,3,6,5,4]\n // 0 - 4 -> d 4. ...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
0
{ "code": "using i64 = long long;\n\nconstexpr int MAX = 128;\n\ni64 sum[2][MAX], dp[2][MAX][2];\n\nclass Solution {\npublic:\n long long maximumScore(vector<vector<int>>& grid) {\n int n = grid.size(), x = 0;\n sum[0][0] = 0;\n for (int i = 0; i < n; ++i) {\n sum[0][i + 1] = sum[0][i] + grid...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
0
{ "code": "class Solution {\npublic:\n long long dp[102][101][2];\n long long f(int i, int j, int fl, vector<vector<int>>& grid, int m) {\n if (m == 1 || j >= m) return 0;\n if (dp[i + 1][j][fl] != -1) return dp[i + 1][j][fl];\n long long ans = f(grid.size() - 1, j + 1, 0, grid, m);\n ...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
2
{ "code": "class Solution {\npublic:\n long long solve1(vector<vector<int>>& grid) {\n const int n = grid.size();\n\n // dp[i][j][color]\n // the maximum score we can get in a grid[0..i][0..j] and grid[i][j] is colored to color \n // (black/white/white_counted).\n //\n // Transition...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
2
{ "code": "typedef long long int ll;\nclass Solution {\npublic:\n ll dp[101][101][101];\n vector<vector<int>> g;\n vector<vector<ll>> pref;\n int R,C;\n ll Score (int col, int cur_len, int max_len) {\n if (max_len <= cur_len) return 0;\n \n return pref[max_len][col] - pref[cur_len]...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
2
{ "code": "class Solution {\n int n;\n vector<vector<vector<long long>>> dp;\n vector<vector<long long>> col_sum;\n\n // returns contribution due to (col-1) column\n long long compute(int col, int curr_len, int max_len) {\n if(curr_len == n || col < 0 || max_len <= curr_len) \n return...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
2
{ "code": "class Solution {\npublic:\n long long maximumScore(vector<vector<int>>& grid) {\n n = grid.size();\n colPSum.resize(n, std::vector<int64_t>(n + 1, 0));\n for (int j = 0; j < n; j++) {\n for(int i = 1; i <= n; i++) {\n colPSum[j][i] = colPSum[j][i - 1] + gri...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
2
{ "code": "#define ll long long\ntypedef vector<ll> vll;\ntypedef vector<vll> vvll;\ntypedef vector<vvll> vvvll;\n\nll func( int col , int fill , int curr , int n , vvll &pref , vvvll &dp )\n{\n if(col == n - 1 )\n {\n if( curr == 1 )\n return 0;\n\n ll tot = fill ? pref[col][fill-1] : 0 ...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
2
{ "code": "#define ll long long\ntypedef vector<ll> vll;\ntypedef vector<vll> vvll;\ntypedef vector<vvll> vvvll;\n\nll func( int col , int fill , int curr , int n , vvll &pref , vvvll &dp )\n{\n if(col == n - 1 )\n {\n if( curr == 1 )\n return 0;\n\n ll tot = fill ? pref[col][fill-1] : 0 ...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
3
{ "code": "typedef long long ll;\nclass Solution {\npublic:\n vector<vector<vector<ll>>> memo; // memo[i][prev][isBigger] is the extra we can get\n // assuming that i is bigger than i-2\n vector<vector<ll>> colSums;\n\n ll colSum(int c, int s, int e){\n if (s > e) return 0;\n if (s == 0) ret...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
3
{ "code": "long long solve(int i,int last,int flag,vector<vector<int>>& grid,vector<vector<long long>>&pre,vector<vector<vector<long long>>>&dp)\n{\n int n=grid.size();\n if(i>n)\n return 0;\n if(dp[i][last][flag]!=-1)return dp[i][last][flag];\n\n long long ans=0;\n \n if(flag)\n {\n ...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
3
{ "code": "template <typename T, typename U, typename V>\nstruct segment_tree {\n vector<T> tree;\n U updater;\n V combiner;\n segment_tree(int n, T default_value, U updater, V combiner) : tree(2 * n, default_value), updater(updater), combiner(combiner) {}\n inline void update(int i, T v) {\n for (tree[i] = u...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
3
{ "code": "template<typename T, typename U, typename V>\nstruct segment_tree {\n vector<T> tree;\n U updater;\n V combiner;\n segment_tree(int n, T default_value, U updater, V combiner) : tree(2 * n, default_value), updater(updater), combiner(combiner) {}\n inline void update(int i, T v) {\n for...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
3
{ "code": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\nusing namespace __gnu_pbds;\nusing namespace std;\nusing ll=long long;\ntypedef tree <pair<ll,ll>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;\n/*\n order_of_key (k)\n find_by_order(k)\n*/\ntemplat...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
3
{ "code": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\nusing namespace __gnu_pbds;\nusing namespace std;\nusing ll=long long;\ntypedef tree <pair<ll,ll>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;\n/*\n order_of_key (k)\n find_by_order(k)\n*/\ntemplat...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
3
{ "code": "enum Direction {UP, DOWN};\n\nclass Solution {\npublic:\n long long maximumScore(vector<vector<int>>& grid) {\n initPrefixScores(grid);\n int n = grid.size();\n vector<vector<vector<long long>>> dp(4, vector<vector<long long>>(128, vector<long long>(2, 0)));\n for (int j = 0;...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
3
{ "code": "class Solution {\npublic:\n long long int sol(vector<vector<int>>& grid,int curr,int prev,int prev2,vector<vector<vector<long long int>>> &dp){\n int n = grid.size();\n if(curr>=n) return 0;\n\n if(dp[curr][prev][prev2] != -1) return dp[curr][prev][prev2];\n long long int fro...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
3
{ "code": "class Solution {//https://leetcode.com/problems/maximum-score-from-grid-operations/solutions/5512718/clean-java-recursive-dp\npublic:\n vector<vector<vector<long>>> dp;\n\n long solve(vector<vector<int>>& grid, int col, int last1, int last2) {\n int n = grid.size();\n \n if (col ...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
3
{ "code": "class Solution {\npublic:\n long long maximumScore(vector<vector<int>>& grid) {\n using LL = long long;\n int n = grid.size();\n vector<vector<vector<LL>>> f(n, vector<vector<LL>>(n + 1, vector<LL>(n + 1, 0)));\n vector<vector<LL>> sum(n, vector<LL>(n));\n for (int i =...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
3
{ "code": "class Solution {\npublic:\n long long solve1(vector<vector<int>>& grid) {\n const int n = grid.size();\n\n // dp[j][black][counted]\n vector<vector<vector<long long>>> dp(n + 1, vector<vector<long long>>(n + 1, vector<long long>(n + 1, 0)));\n vector<vector<long long>> pref_sum(n + 1...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
3
{ "code": "class Solution {\npublic:\n long long maximumScore(vector<vector<int>>& grid) {\n // dp[i][j][k] -> at jth column, first k out of i values were black, last l values haven't been reached.\n // dp[i][j][k][l] = (dp[i][j-1][i] + sum(grid[k+1 to i][j] + sum(grid[i-l to k+1][j-1]))\n\n /...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
3
{ "code": "class Solution {\npublic:\n\n long long max(long long a, long long b) { \n return (a > b ? a : b);\n }\n\n\n long long maximumScore(vector<vector<int>>& arr) {\n\n //vector<vector<vector<long long>>> dp(arr[0].size(), vector<vector<long long>>(arr.size()+1, vector<long long>(arr.size()+1))...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
3
{ "code": "class Solution {\npublic:\n #define ll long long\n long long maximumScore(vector<vector<int>>& grid) {\n int n=(int)grid.size();\n if(n==1) return 0;\n vector<vector<vector<ll>> >dp(n,vector<vector<ll> >(n+1,vector<ll>(n+1)));\n vector<vector<ll> >prefix(n,vector<ll>(n+1))...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
3
{ "code": "class Solution {\n // Suppose that for the j-th column, we color the first a[j] rows. That is, pick (a[j], j)\n // The cell grid[i][j] is added to the result if (i > a[j] and (i <= a[j-1] or i <= a[j+1]))\n //\n // f(j, i, k) the maximum score in the first j-1 columns, if (a[j]=i and a[j-1]=k)\n stati...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
3
{ "code": "class Solution {\n // Suppose that for the j-th column, we color the first a[j] rows. That is, pick (a[j], j)\n // The cell grid[i][j] is added to the result if (i > a[j] and (i <= a[j-1] or i <= a[j+1]))\n //\n // f(j, i, k) the maximum score in the first j-1 columns, if (a[j]=i and a[j-1]=k)\n stati...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
3
{ "code": "class Solution {\n // Suppose that for the j-th column, we color the first a[j] rows. That is, pick (a[j], j)\n // The cell grid[i][j] is added to the result if (i > a[j] and (i <= a[j-1] or i <= a[j+1]))\n //\n // f(j, i, k) the maximum score in the first j-1 columns, if (a[j]=i and a[j-1]=k)\n stati...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
3
{ "code": "class Solution {\n // Suppose that for the j-th column, we color the first a[j] rows. That is, pick (a[j], j)\n // The cell grid[i][j] is added to the result if (i > a[j] and (i <= a[j-1] or i <= a[j+1]))\n //\n // f(j, i, k) the maximum score in the first j-1 columns, if (a[j]=i and a[j-1]=k)\n stati...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
3
{ "code": "class Solution {\n // Suppose that for the j-th column, we color the first a[j] rows. That is, pick (a[j], j)\n // The cell grid[i][j] is added to the result if (i > a[j] and (i <= a[j-1] or i <= a[j+1]))\n //\n // f(j, i, k) the maximum score in the first j-1 columns, if (a[j]=i and a[j-1]=k)\n stati...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
3
{ "code": "class Solution {\n // Suppose that for the j-th column, we color the first a[j] rows. That is, pick (a[j], j)\n // The cell grid[i][j] is added to the result if (i > a[j] and (i <= a[j-1] or i <= a[j+1]))\n //\n // f(j, i, k) the maximum score in the first j-1 columns, if (a[j]=i and a[j-1]=k)\n stati...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
3
{ "code": "class Solution {\npublic:\n long long maximumScore(vector<vector<int>>& grid) {\n const int MAX_SIZE = 110; // Maximum size of the grid plus extra space\n const long long INF = LLONG_MAX; // Represents infinity for comparison\n vector<vector<long long>> dp(MAX_SIZE, vector<long long...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
3
{ "code": "class Solution {\npublic:\n long long maximumScore(vector<vector<int>>& grid) {\n const int MAX_SIZE = 109; // Maximum size of the grid plus extra space\n const long long INF = LLONG_MAX; // Represents infinity for comparison\n\n vector<vector<long long>> dp(MAX_SIZE, vector<long lo...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
3
{ "code": "class Solution {\npublic:\n long long maximumScore(vector<vector<int>>& grid) {\n const int MAX_SIZE = 109;\n const long long INF= LLONG_MAX;\n\n vector<vector<long long>> dp(MAX_SIZE, vector<long long>(MAX_SIZE, -INF));\n vector<vector<long long>> maxPrefix(MAX_SIZE, vector<...
3,470
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
3
{ "code": "class Solution {\npublic:\n long long maximumScore(vector<vector<int>>& grid) {\n const int MAX_SIZE = 109; // Maximum size of the grid plus extra space\n const long long INF = LLONG_MAX; // Represents infinity for comparison\n\n vector<vector<long long>> dp(MAX_SIZE, vector<long lo...
3,508
<p>You are given two positive integers <code>n</code> and <code>k</code>.</p> <p>You can choose <strong>any</strong> bit in the <strong>binary representation</strong> of <code>n</code> that is equal to 1 and change it to 0.</p> <p>Return the <em>number of changes</em> needed to make <code>n</code> equal to <code>k</c...
0
{ "code": "#include <iostream>\n#include <algorithm>\n#include <vector>\n\nclass Solution {\npublic:\n int minChanges(int n, int k) {\n if (k > n) {\n return -1; // It's impossible to make n greater than or equal to k by any means\n }\n\n int changes = 0;\n while (n > 0 || k ...
3,508
<p>You are given two positive integers <code>n</code> and <code>k</code>.</p> <p>You can choose <strong>any</strong> bit in the <strong>binary representation</strong> of <code>n</code> that is equal to 1 and change it to 0.</p> <p>Return the <em>number of changes</em> needed to make <code>n</code> equal to <code>k</c...
0
{ "code": "class Solution {\npublic:\n int minChanges(int n, int k) {\n if((n&k)!=k)return -1;\n int a=n^k;\n \n return __builtin_popcount(a);\n }\n};", "memory": "7800" }
3,508
<p>You are given two positive integers <code>n</code> and <code>k</code>.</p> <p>You can choose <strong>any</strong> bit in the <strong>binary representation</strong> of <code>n</code> that is equal to 1 and change it to 0.</p> <p>Return the <em>number of changes</em> needed to make <code>n</code> equal to <code>k</c...
0
{ "code": "class Solution {\npublic:\n int minChanges(int n, int k) {\n if((n&k)!=k){\n return -1;\n }\n else{\n return __builtin_popcount(n-k);\n }\n }\n};", "memory": "7900" }
3,508
<p>You are given two positive integers <code>n</code> and <code>k</code>.</p> <p>You can choose <strong>any</strong> bit in the <strong>binary representation</strong> of <code>n</code> that is equal to 1 and change it to 0.</p> <p>Return the <em>number of changes</em> needed to make <code>n</code> equal to <code>k</c...
0
{ "code": "class Solution {\npublic:\n int minChanges(int n, int k) {\n int ans=0;\n for(int i=0;i<32;i++){\n int nb=((n>>i)&1);\n int kb=((k>>i)&1);\n\n if(nb!=kb){\n if(nb==0){\n return -1;\n }else{\n ...
3,508
<p>You are given two positive integers <code>n</code> and <code>k</code>.</p> <p>You can choose <strong>any</strong> bit in the <strong>binary representation</strong> of <code>n</code> that is equal to 1 and change it to 0.</p> <p>Return the <em>number of changes</em> needed to make <code>n</code> equal to <code>k</c...
0
{ "code": "class Solution {\npublic:\n int minChanges(int n, int k) {\n int cnt=0,s,t,pos=0;\n int N=n,K=k;\n while(n){\n s=n&1;\n t=k&1;\n if((s^t)==1 && s==1){\n N&=(~(1<<pos));\n cnt++;\n }\n n>>=1;\n k>...
3,508
<p>You are given two positive integers <code>n</code> and <code>k</code>.</p> <p>You can choose <strong>any</strong> bit in the <strong>binary representation</strong> of <code>n</code> that is equal to 1 and change it to 0.</p> <p>Return the <em>number of changes</em> needed to make <code>n</code> equal to <code>k</c...
0
{ "code": "class Solution {\npublic:\n int minChanges(int n, int k) {\n int ans = 0;\n for(int i = 0 ; i < 32 ; i++)\n {\n if(n & (1 << i))\n {\n if(!(k & (1 << i)))\n {\n ans++;\n }\n }\n ...
3,508
<p>You are given two positive integers <code>n</code> and <code>k</code>.</p> <p>You can choose <strong>any</strong> bit in the <strong>binary representation</strong> of <code>n</code> that is equal to 1 and change it to 0.</p> <p>Return the <em>number of changes</em> needed to make <code>n</code> equal to <code>k</c...
0
{ "code": "class Solution {\npublic:\n int minChanges(int n, int k) {\n if((n&k)!=k) return -1;\n else{\n int num=n^k;;\n int ans= 0;\n while(num>0){\n ans++;\n num= num & (num-1);\n }\n return ans;\n }\n }...
3,508
<p>You are given two positive integers <code>n</code> and <code>k</code>.</p> <p>You can choose <strong>any</strong> bit in the <strong>binary representation</strong> of <code>n</code> that is equal to 1 and change it to 0.</p> <p>Return the <em>number of changes</em> needed to make <code>n</code> equal to <code>k</c...
2
{ "code": "class Solution {\npublic:\n string itob(int n){\n string ans;\n while(n>0){\n int last=n%2;\n ans.push_back(last+'0');\n n/=2;\n }\n return ans;\n }\n\n int minChanges(int n, int k) {\n if(n==k){\n return 0;\n }\...
3,508
<p>You are given two positive integers <code>n</code> and <code>k</code>.</p> <p>You can choose <strong>any</strong> bit in the <strong>binary representation</strong> of <code>n</code> that is equal to 1 and change it to 0.</p> <p>Return the <em>number of changes</em> needed to make <code>n</code> equal to <code>k</c...
3
{ "code": "class Solution {\npublic:\n int minChanges(int n, int k) {\n if (n == k) return 0;\n int ans= 0;\n string binary_n = bitset<32>(n).to_string();\n string binary_k = bitset<32>(k).to_string();\n for (int i = 0 ; i < 32; i++) {\n if (binary_n[i] == '0' && binar...
3,508
<p>You are given two positive integers <code>n</code> and <code>k</code>.</p> <p>You can choose <strong>any</strong> bit in the <strong>binary representation</strong> of <code>n</code> that is equal to 1 and change it to 0.</p> <p>Return the <em>number of changes</em> needed to make <code>n</code> equal to <code>k</c...
3
{ "code": "class Solution {\npublic:\n int minChanges(int n, int k) {\n if (n == k) return 0;\n int ans= 0;\n string binary_n = bitset<32>(n).to_string();\n string binary_k = bitset<32>(k).to_string();\n for (int i = 0 ; i < 32; i++) {\n if (binary_n[i] == '0' && binar...
3,508
<p>You are given two positive integers <code>n</code> and <code>k</code>.</p> <p>You can choose <strong>any</strong> bit in the <strong>binary representation</strong> of <code>n</code> that is equal to 1 and change it to 0.</p> <p>Return the <em>number of changes</em> needed to make <code>n</code> equal to <code>k</c...
3
{ "code": "class Solution {\npublic:\n int minChanges(int n, int k) {\n if ((n & k) != k) {\n return -1;\n }\n int ans = 0;\n \n string n_bin = bitset<32>(n).to_string();\n string k_bin = bitset<32>(k).to_string();\n \n for (char c : n_bin)...
3,508
<p>You are given two positive integers <code>n</code> and <code>k</code>.</p> <p>You can choose <strong>any</strong> bit in the <strong>binary representation</strong> of <code>n</code> that is equal to 1 and change it to 0.</p> <p>Return the <em>number of changes</em> needed to make <code>n</code> equal to <code>k</c...
3
{ "code": "class Solution {\npublic:\n int minChanges(int n, int k) {\n if ((n & k) != k) {\n return -1;\n }\n\n int res = 0;\n \n string n_bin = bitset<32>(n).to_string();\n string k_bin = bitset<32>(k).to_string();\n \n for (char c : n_bin) {\n ...
3,508
<p>You are given two positive integers <code>n</code> and <code>k</code>.</p> <p>You can choose <strong>any</strong> bit in the <strong>binary representation</strong> of <code>n</code> that is equal to 1 and change it to 0.</p> <p>Return the <em>number of changes</em> needed to make <code>n</code> equal to <code>k</c...
3
{ "code": "class Solution {\npublic:\n int minChanges(int n, int k) {\n if(n==k) return 0;\n string x1=toBin(n);\n string x2=toBin(k);\n if(x1.size()>x2.size()){\n int diff=x1.size()-x2.size();\n x2=string(diff,'0')+x2;\n }\n else if(x1.size()<x2.size...
3,508
<p>You are given two positive integers <code>n</code> and <code>k</code>.</p> <p>You can choose <strong>any</strong> bit in the <strong>binary representation</strong> of <code>n</code> that is equal to 1 and change it to 0.</p> <p>Return the <em>number of changes</em> needed to make <code>n</code> equal to <code>k</c...
3
{ "code": "class Solution {\npublic:\n int minChanges(int n, int k) {\n string bn = bitset<32>(n).to_string();\n string bk = bitset<32>(k).to_string();\n bn = bn.substr(bn.find('1'));\n bk = bk.substr(bk.find('1'));\n if (bn.length() < bk.length()) {\n return -1;\n ...
3,508
<p>You are given two positive integers <code>n</code> and <code>k</code>.</p> <p>You can choose <strong>any</strong> bit in the <strong>binary representation</strong> of <code>n</code> that is equal to 1 and change it to 0.</p> <p>Return the <em>number of changes</em> needed to make <code>n</code> equal to <code>k</c...
3
{ "code": "class Solution {\npublic:\n vector<int>bitArray(int n){\n vector<int>ans;\n while(n){\n ans.push_back(n%2);\n n=n/2;\n }\n return ans;\n }\n int minChanges(int n, int k) {\n if(n==k)\n return 0; \n vector<int>nth,kth;\n ...