id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
3,263 | <p>You are given an array of integers <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need t... | 3 | {
"code": "class Solution {\npublic:\n int minimumCost(vector<int>& nums) {\n \n vector<int> arr;\n \n for(int i = 1 ; i < nums.size() ; i++){\n arr.push_back(nums[i]);\n }\n\n sort(arr.begin() , arr.end());\n \n int sum = nums[0] + arr[0] + arr[1];\n \n return sum;... |
3,263 | <p>You are given an array of integers <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need t... | 3 | {
"code": "class Solution {\npublic:\n int minimumCost(vector<int>& nums) {\n std::priority_queue<int> pq;\n for (int i = 1; i < nums.size(); i++)\n {\n int num = nums[i];\n if (pq.size() < 2)\n {\n pq.push(num);\n } else if (num < pq.... |
3,263 | <p>You are given an array of integers <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need t... | 3 | {
"code": "class Solution {\npublic:\n int minimumCost(vector<int>& nums) {\n priority_queue<int> pq;\n for(int i=1; i<nums.size(); i++){\n if(pq.size()<2){\n pq.push(nums[i]);\n }else{\n if(pq.top() > nums[i]){\n pq.pop();\n ... |
3,263 | <p>You are given an array of integers <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need t... | 3 | {
"code": "class Solution {\npublic:\n int minimumCost(vector<int>& nums) {\n if(nums.size()<3)\n return 0;\n int cost = 0;\n for(int i = 0 ;i<nums.size(); i++)\n {\n cost = nums[0];\n sort(nums.begin()+1,nums.end());\n cost = cost + nums[1] + num... |
3,263 | <p>You are given an array of integers <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need t... | 3 | {
"code": "class Solution \n{\npublic:\n int minimumCost(vector<int>& nums) \n {\n int ans = nums[0];\n priority_queue<int> pq;\n for(int i=1 ; i<nums.size() ; i++)\n {\n pq.push(nums[i]);\n if(pq.size()>2)\n pq.pop();\n }\n ans += p... |
3,263 | <p>You are given an array of integers <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need t... | 3 | {
"code": "class Solution \n{\npublic:\n int minimumCost(vector<int>& nums) \n {\n int ans = nums[0];\n priority_queue<int> pq;\n for(int i=1 ; i<nums.size() ; i++)\n {\n pq.push(nums[i]);\n if(pq.size()>2)\n pq.pop();\n }\n ans += p... |
3,263 | <p>You are given an array of integers <code>nums</code> of length <code>n</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1,2,3]</code> is <code>1</code> while the cost of <code>[3,4,1]</code> is <code>3</code>.</p>
<p>You need t... | 3 | {
"code": "class Solution \n{\npublic:\n int minimumCost(vector<int>& nums) \n {\n int ans = nums[0];\n priority_queue<int> pq;\n for(int i=1 ; i<nums.size() ; i++)\n {\n pq.push(nums[i]);\n if(pq.size()>2)\n pq.pop();\n }\n ans += p... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 0 | {
"code": "class Solution {\npublic:\n int bit(int val)\n {\n return bitset<10>(val).count();\n }\n bool canSortArray(vector<int>& nums) \n {\n int n = nums.size(), flag = true;\n while(flag)\n {\n flag = false;\n for(int i=0;i<n-1;i++)\n {\n... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 0 | {
"code": "class Solution {\npublic:\n int setbits(int n){\n int count = 0;\n while(n>0){\n int n1 = n-1;\n n = n&n1;\n count++;\n }\n return count;\n }\n bool canSortArray(vector<int>& nums) {\n int n = nums.size();\n for(int i = 0;i... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 0 | {
"code": "class Solution {\npublic:\n // Bubble Sort, Time : O(n^2), Space : O(1)\n bool canSortArray(vector<int>& nums) {\n int n = nums.size();\n\n for(int i = 0; i < n-1; i++){\n bool swapped = true;\n for(int j = 0; j < n-1-i; j++){\n int bitCount1 = __bui... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 0 | {
"code": "class Solution {\npublic:\n bool canSortArray(vector<int>& nums) {\n int prvSB=0;\n int pMa=INT_MIN; \n int cMa=INT_MIN; \n int cMn=INT_MAX; \n\n for(const int num:nums) \n {\n const int sb = __builtin_popcount(num);\n if(sb!=prvSB) \n ... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 0 | {
"code": "class Solution {\npublic:\n bool canSortArray(vector<int>& nums) {\n\n int n = nums.size();\n \n for(int i = 0; i < n; i++){\n for(int j = 1; j < n; j++){\n if(nums[j] < nums[j-1]){\n int s1 = __builtin_popcount(nums[j]);\n ... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 0 | {
"code": "class Solution {\npublic:\n bool canSortArray(vector<int>& nums) {\n int count1 = 0;\n int count2 = 0;\n int temp1 = 0;\n int temp2 = 0;\n for(int i=0;i<nums.size();i++)\n {\n int temp = nums[i];\n while(temp)\n {\n ... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 1 | {
"code": "class Solution {\npublic:\n int bit(int val)\n {\n return bitset<10>(val).count();\n }\n bool canSortArray(vector<int>& nums) \n {\n int n = nums.size(), flag = true;\n while(flag)\n {\n flag = false;\n for(int i=0;i<n-1;i++)\n {\n... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 2 | {
"code": "class Solution {\npublic:\n Solution() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n }\n bool canSortArray(vector<int>& nums) {\n\n int n = nums.size() , i = 0 , j = 0 , prevMax = 0;\n while(i < n) {\n\n int currMax = 0 , c... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 2 | {
"code": "// LeetCode-3011: https://leetcode.com/problems/find-if-array-can-be-sorted/\n\n#include <vector>\n#include <utility>\n#include <cassert>\n\nusing namespace std;\n\nclass Solution {\npublic:\n friend class SolutionTest;\n\n bool canSortArray(vector<int>& nums) {\n if (nums.size() <= 1) {\n ... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 2 | {
"code": "// LeetCode-3011: https://leetcode.com/problems/find-if-array-can-be-sorted/\n\n#include <vector>\n#include <utility>\n#include <cassert>\n\nusing namespace std;\n\nclass Solution {\npublic:\n friend class SolutionTest;\n\n bool canSortArray(vector<int>& nums) {\n if (nums.size() <= 1) {\n ... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 2 | {
"code": " class Solution {\npublic:\n bool canSortArray(vector<int>& nums) {\n int n = nums.size();\n vector<int> bitCount(n,0);\n for(int i=0; i<n; i++) \n bitCount[i] = __builtin_popcount(nums[i]);\n \n int prevMax = 0;\n int curMax,curMin;\n \n ... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 2 | {
"code": "class Solution {\npublic:\n bool canSortArray(vector<int>& nums) {\n int n = nums.size();\n vector<int> count;\n for(auto n:nums){\n int tmp = 0;\n while(n>0){\n if(n&1) tmp++;\n n = n>>1;\n }\n count.push_bac... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 2 | {
"code": "class Solution {\n inline int countBits(int num) {\n int result = 0;\n\n while (num) {\n if (num & 1) result++ ;\n num >>= 1;\n }\n\n return result;\n }\npublic:\n bool canSortArray(vector<int>& nums) {\n int start = 0;\n int end = 0;... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 2 | {
"code": "class Solution {\n inline int countBits(int num) {\n int result = 0;\n\n while (num) {\n if (num & 1) result++ ;\n num >>= 1;\n }\n\n return result;\n }\npublic:\n bool canSortArray(vector<int>& nums) {\n int start = 0;\n int end = 0;... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 2 | {
"code": "int count_bits(int n) {\n int count = 0;\n while (n) {\n count += n & 1;\n n >>= 1;\n }\n return count;\n}\n\nclass Solution {\npublic:\n bool canSortArray(vector<int>& nums) {\n int start = 0;\n for (int end = 0; end < nums.size(); end++) {\n if (count... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 2 | {
"code": "int count_bits(int n) {\n int count = 0;\n while (n) {\n count += n & 1;\n n >>= 1;\n }\n return count;\n}\n\nclass Solution {\npublic:\n bool canSortArray(vector<int>& nums) {\n int start = 0;\n for (int end = 0; end < nums.size(); end++) {\n if (count... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 2 | {
"code": "class Solution {\npublic:\n bool canSortArray(vector<int>& nums) {\n vector<int> v;\n for(int i=0;i<nums.size();i++){\n v.push_back(nums[i]);\n }\n sort(v.begin(),v.end());\n for(int j=0;j<nums.size();j++){\n for(int i=0;i<nums.size()-1;i++){\n if(nums... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 2 | {
"code": "class Solution {\npublic:\n bool canSortArray(vector<int>& nums) {\n int cmx=-1,mx=-1,curr=-1;\n int dp[257];\n for(int i=1;i<257;i++) dp[i]=dp[i/2]+i%2;\n for(int i=0;i<nums.size();i++){\n if(curr!=dp[nums[i]]){\n mx=cmx;\n cmx=nums[i... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 2 | {
"code": "class Solution {\npublic:\n void bubbleSort(vector<pair<int,int>>& arr) {\n int n = arr.size();\n for (int i=0;i<n-1;++i) {\n for (int j=0;j<n-i-1; ++j) {\n if (arr[j].first> arr[j+1].first && arr[j].second==arr[j+1].second) {\n swap(arr[j], arr[j+1]);\n ... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 2 | {
"code": "class Solution {\npublic:\n void bubbleSort(vector<pair<int,int>>& arr) {\n int n = arr.size();\n for (int i=0;i<n-1;++i) {\n for (int j=0;j<n-i-1; ++j) {\n if (arr[j].first> arr[j+1].first && arr[j].second==arr[j+1].second) {\n swap(arr[j], arr[j+1]);\n ... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n// __builtin_popcount(n);\n bool canSortArray(vector<int>& nums) {\n vector<int>temp=nums;\n sort(temp.begin(),temp.end());\n int i=0;\n int prev=0;\n while(i<nums.size()){\n prev=i;\n int cnt1=__builtin_popcount(num... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n bool canSortArray(vector<int>& inputNums) {\n const int N = inputNums.size();\n\n vector<int> nums = inputNums;\n\n int l = 0;\n while(l < N){\n int r = l + 1;\n while(r < N && popcount((unsigned int)nums[l]) == popcount((unsi... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n bool canSortArray(vector<int>& nums) {\n vector<int> v(nums.size(),0);\n for(int i=0;i<nums.size();i++){\n int count = __builtin_popcount(nums[i]);\n v[i]=count;\n }\n int i=0;\n while(i<nums.size()){\n int j... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n int cnt(int n)\n {\n int c=0;\n for(int i=0;i<32;i++)\n {\n if(1 & (n>>i))\n c++;\n }\n return c;\n }\n bool canSortArray(vector<int>& nums) {\n vector<int> org=nums;\n sort(org.begin(), org.e... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n bool canSortArray(vector<int>& nums) {\n\n int i=0;\n vector<int>v;\n for(auto it: nums)\n v.push_back(__builtin_popcount(it));\n\n while(i<nums.size())\n {\n int j=i;\n while(j+1<nums.size() && v[j]==v[j+1])... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n bool canSortArray(vector<int>& nums) {\n \n int n=nums.size();\n vector<int>arr=nums;\n \n vector<int>x={75,34,30};\n vector<int>y={9,28,18,26,11};\n vector<int>z={4,3,1};\n vector<int>ab={212, 130, 58};\n vector<... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n bool canSortArray(vector<int>& nums) {\n int n = nums.size();\n vector<int>tmp,maxu,minu;\n for(int i=0;i<n;i++) tmp.push_back(__builtin_popcount(nums[i]));\n int mino = INT_MAX, maxo = INT_MIN;\n for(int i=0;i<n-1;i++){\n if(tmp[... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n bool canSortArray(vector<int>& nums) {\n int n = nums.size();\n vector<int>tmp,maxu,minu;\n for(int i=0;i<n;i++) tmp.push_back(__builtin_popcount(nums[i]));\n int mino = INT_MAX, maxo = INT_MIN;\n for(int i=0;i<n-1;i++){\n if(tmp[... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "int numofbits(int n){\n int setbits=0;\n for(int i=8;i>=0;i--){\n if(((n>>i)&1)==1)setbits++;\n }\n return setbits;\n}\n\nbool check(vector<int>& nums){\n int n=nums.size();\n for(int i=1;i<n;i++){\n if(nums[i]<nums[i-1])return false;\n }\n return true;\n}\n\nclass Sol... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n bool canSortArray(vector<int>& nums) {\n string s=to_string(nums[0]);\n int prevmax=INT_MIN;\n int curmax=nums[0];\n int sum1=__builtin_popcount(nums[0]);\n for(int i=1;i<nums.size();i++){\n string s1=to_string(nums[i]);\n ... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n int bits(int num){\n int ans=0;\n while(num){\n if(num&1) ans++;;\n num>>=1;\n }\n return ans;\n }\n bool canSortArray(vector<int>& nums) {\n vector<int> bitcnt(nums.size());\n for(int i=0;i<nums.size();++i... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n\n int countSetBits(int n){\n int ans = 0;\n while(n){\n if(n&1) ans++;\n n>>=1;\n }\n return ans;\n }\n\n bool isSorted(vector<int>&nums){\n for(int i=0; i<nums.size()-1; i++){\n if(nums[i]>nums[i+1]) r... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "int setBits(int num) {\n int ret = 0;\n for (auto i = 0; i <= 8; ++i) {\n if (num & (1 << i)) {\n ++ret;\n }\n }\n return ret;\n}\n\nbool _subr(vector<int> &nums, int num, int at) {\n while (at > 0 && nums[at - 1] > num) {\n if (setBits(num) != setBits(nums[at... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n int getbitone(int x)\n {\n int res = 0;\n while (x>0)\n {\n int tmp = x%2;\n if (tmp) res++;\n x /= 2;\n }\n return res;\n }\n bool canSortArray(vector<int>& nums) {\n unordered_map<int,int> m... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n bool canSortArray(vector<int>& nums) {\n int n= nums.size();\n unordered_map<int, int> pre;\n for(int i=0; i<n; i++){\n int c=0;\n for(int j=0; j<10; j++){\n if(nums[i]&1<<j){\n c++;\n ... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n bool canSortArray(vector<int>& nums) {\n map<int,int> counter;\n for(int i=0;i<nums.size();i++)\n {\n counter[nums[i]]=__builtin_popcount(nums[i]);\n }\n\nfor(int j=0;j<nums.size()-1;j++)\n{\n for(int k=0;k<nums.size()-1;k++)\n {\n... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n bool check(vector<int>& nums) {\n for(int i = 0; i < nums.size() - 1; i++) {\n if(nums[i] > nums[i+1]) {\n return false;\n }\n }\n return true;\n }\n\n bool canSortArray(vector<int>& nums) {\n map<int, int... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n bool canSortArray(vector<int>& nums) {\n unordered_map<int,int> mp;\n int n=nums.size();\n for(auto it:nums){\n mp[it]=__builtin_popcount(it);\n }\n\n for (int i = 0; i < n - 1; i++) {\n for (int j = 0; j < n - i - 1; j... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n bool canSortArray(vector<int>& nums) {\n unordered_map<int,int> bits;\n for (auto i:nums){\n int d = i;\n int count = 0;\n while (d>0){\n count += (1&d);\n d=d>>1;\n }\n bits[i]=count;\... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n int find_set_bit(int n){\n\n int count = 0;\n\n while(n > 0 ){\n\n n = n&(n-1);\n count++;\n }\n\n\n return count;\n\n }\n\n bool check(vector<int>& nums,vector<int>& sorted_nums, int i , int j){\n\n vector <int> ... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\nint solve(int num){\n int cnt = 0;\n while(num!=0){\n if(num&1==1)cnt++;\n num = num>>1;\n }\n return cnt;\n }\n bool canSortArray(vector<int>& nums) {\n int n = nums.size();\n vector<int> tt = nums;\n v... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n bool canSortArray(vector<int>& nums) {\n int n=nums.size();\n unordered_map<int,int>mp;\n for(int i=0;i<n;i++){\n int a=nums[i];\n int c=0;\n while(a>0){\n int rem=a%2;\n if(rem&1)c++;\n a/=2;\n }... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\n\n bool check(vector<int> &nums,vector<int> &temp,int i,int j){\n vector<int> temp1,temp2;\n for(int k=i;k<=j;k++){\n temp1.push_back(nums[k]);\n temp2.push_back(temp[k]);\n }\n sort(temp1.begin(),temp1.end());\n return temp1==te... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n int setBits(int n) {\n int bits = 0;\n while (n) {\n bits += (n & 1);\n n >>= 1;\n }\n return bits;\n }\n bool canSortArray(vector<int>& nums) {\n vector<int> sorted = nums;\n sort(sorted.begin(), sorted.en... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\nprivate:\n int setBits(int n)\n {\n int res = 0;\n while (n)\n {\n if (n % 2)\n {\n res++;\n }\n n /= 2;\n }\n\n return res;\n }\n\npublic:\n bool canSortArray(vector<int>& nums) {\n ... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n\n int count(int num){\n int c = 0;\n for(int i=0;i<32;i++){\n if(num & (1<<i)) c++;\n }\n return c;\n }\n\n bool canSortArray(vector<int>& nums) {\n int n = nums.size();\n\n unordered_map<int, int> mpp;\n for(i... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n bool canSortArray(vector<int>& nums) {\n map<int, int> bit_max;\n map<int, int> bit_min;\n vector<int> set_bits(nums.size(), 0);\n vector<int> bit_color(nums.size(), 0);\n int last_set_bit_num = -1;\n int current_color = -1;\n ... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\nint solve(int num){\n int cnt = 0;\n while(num!=0){\n if(num&1==1)cnt++;\n num = num>>1;\n }\n return cnt;\n }\n bool canSortArray(vector<int>& nums) {\n int n = nums.size();\n vector<int> tt = nums;\n v... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n bool canSortArray(vector<int>& nums) {\n \n vector<int> temp = nums;\n \n sort(temp.begin(), temp.end());\n \n int n = nums.size();\n \n vector<int> temp2(n, 0);\n int i = 0;\n unordered_map<int, int> order... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n int setBits(int num){\n int cnt = 0;\n\n for(int i=0; i<=30; i++){\n if(num & (1 << i)) cnt++;\n }\n return cnt;\n }\n bool canSortArray(vector<int>& nums) {\n int n = nums.size();\n multiset<int>set;\n vector<... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n int countBits(int x) {\n int res = 0;\n while (x > 0) {\n if (x & 1) res++;\n x >>= 1;\n }\n return res;\n }\n bool canSortArray(vector<int>& nums) {\n // argsort vector\n vector<pair<int, int>> v;\n ... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass Solution {\npublic:\n bool canSortArray(vector<int>& a) {\n\n priority_queue<int,vector<int>,greater<int>>pq;\n priority_queue<int>pq1;\n vector<int>stb(a.size());\n\n for(int i=0;i<a.size();i++){\n stb[i]=__b... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n bool canSortArray(vector<int>& nums) {\n int n=nums.size();\n vector<int>bits(n,0);\n\n priority_queue<int,vector<int>,greater<>>pq;\n\n for(int i=0;i<n;i++){\n int bit=0;\n int curr=nums[i];\n pq.push(curr);\n\n ... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n int bitCount(int n)\n {\n int cnt = 0;\n while(n > 0) {\n if (n%2 == 1) cnt++;\n n/=2;\n }\n\n return cnt;\n }\n bool canSortArray(vector<int>& nums) {\n bool ret = true;\n vector<vector<int>*> arr;\n\n ... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n bool canSortArray(vector<int>& nums){\n int n=nums.size();\n map<int,int> mp;\n for(int i=0; i<n; i++){\n mp[i]=__builtin_popcount(nums[i]);\n }\n vector<int> res=nums;\n int t=n;\n while(t--){\n for(int i=0; i<n-1; i++){\n if(mp[i]==mp[i+1]){\n ... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n bool canSortArray(vector<int>& nums) {\n // vector<int>v=nums;\n \n unordered_map<int,int>mp,mpp;\n for(int i=0;i<nums.size();i++){\n int cnt=0;\n int x=nums[i];\n for(int j=0;j<32;j++){\n if((x>>j)&1... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n bool canSortArray(vector<int>& nums) {\n \n vector<pair<int,int>>num;\n\n for(int i = 0;i<nums.size();i++)\n {\n int setbit = 0;\n for(int j = 0;j<32;j++)\n {\n int bit = (nums[i]>>j)&1;\n ... |
3,291 | <p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code>.</p>
<p>In one <strong>operation</strong>, you can swap any two <strong>adjacent</strong> elements if they have the <strong>same</strong> number of <span data-keyword="set-bit">set bits</span>. You are allowed t... | 3 | {
"code": "class Solution {\npublic:\n bool canSortArray(vector<int>& nums) {\n vector<int> v,v2,nums2;\n nums2=nums;\n for (int num : nums) {\n v.push_back(__builtin_popcount(num));\n // cout << __builtin_popcount(num) << \" \";\n }\n sort(nums2.begin(),nums... |
3,244 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> containing <strong>positive</strong> integers.</p>
<p>Your task is to <strong>minimize</strong> the length of <code>nums</code> by performing the following operations <strong>any</strong> number of times (including zero):</p>
<ul>
<li>Sele... | 0 | {
"code": "class Solution {\npublic:\n int minimumArrayLength(vector<int>& nums) {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n int min_v = INT_MAX;\n for (int i = 0; i < nums.size(); i++) {\n min_v = min(min_v, nums[i]);\n } \n ... |
3,244 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> containing <strong>positive</strong> integers.</p>
<p>Your task is to <strong>minimize</strong> the length of <code>nums</code> by performing the following operations <strong>any</strong> number of times (including zero):</p>
<ul>
<li>Sele... | 0 | {
"code": "class Solution {\npublic:\n int minimumArrayLength(vector<int>& nums) {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n int min_v = INT_MAX;\n for (int i = 0; i < nums.size(); i++) {\n min_v = min(min_v, nums[i]);\n } \n ... |
3,244 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> containing <strong>positive</strong> integers.</p>
<p>Your task is to <strong>minimize</strong> the length of <code>nums</code> by performing the following operations <strong>any</strong> number of times (including zero):</p>
<ul>
<li>Sele... | 0 | {
"code": "class Solution {\npublic:\n int minimumArrayLength(vector<int>& nums) {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n int min_v = INT_MAX;\n for (int i = 0; i < nums.size(); i++) {\n min_v = min(min_v, nums[i]);\n } \n ... |
3,244 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> containing <strong>positive</strong> integers.</p>
<p>Your task is to <strong>minimize</strong> the length of <code>nums</code> by performing the following operations <strong>any</strong> number of times (including zero):</p>
<ul>
<li>Sele... | 0 | {
"code": "class Solution {\npublic:\n int minimumArrayLength(vector<int>& nums) {\n int minele=*min_element(nums.begin(),nums.end());\n int cnt=0;\n for(int i=0;i<nums.size();i++){\n if(nums[i]==minele){\n cnt++;\n }\n }\n if(cnt==1){\n return 1;\... |
3,244 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> containing <strong>positive</strong> integers.</p>
<p>Your task is to <strong>minimize</strong> the length of <code>nums</code> by performing the following operations <strong>any</strong> number of times (including zero):</p>
<ul>
<li>Sele... | 0 | {
"code": "class Solution {\npublic:\n int minimumArrayLength(vector<int>& A) {\n int v = *min_element(A.begin(), A.end());\n for (int x : A)\n if (x % v > 0)\n return 1;\n int cnt = count(A.begin(), A.end(), v);\n return (cnt + 1) / 2;\n }\n};\n// [1,1,1,1]... |
3,244 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> containing <strong>positive</strong> integers.</p>
<p>Your task is to <strong>minimize</strong> the length of <code>nums</code> by performing the following operations <strong>any</strong> number of times (including zero):</p>
<ul>
<li>Sele... | 0 | {
"code": "class Solution {\npublic:\n int minimumArrayLength(vector<int>& nums) \n {\n // minimum element\n auto minnum = *min_element (nums.begin(), nums.end());\n\n // as long as there is at least one element that is not a multiple of minnum, then the array can be reduced to a size of 1.... |
3,244 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> containing <strong>positive</strong> integers.</p>
<p>Your task is to <strong>minimize</strong> the length of <code>nums</code> by performing the following operations <strong>any</strong> number of times (including zero):</p>
<ul>
<li>Sele... | 1 | {
"code": "class Solution {\npublic:\n int minimumArrayLength(vector<int>& nums) {\n int mini = INT_MAX, freq = 0;\n for(auto x : nums) {\n if(mini > x) {\n mini = x;\n freq = 0;\n }\n if(x == mini) {\n freq++;\n ... |
3,244 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> containing <strong>positive</strong> integers.</p>
<p>Your task is to <strong>minimize</strong> the length of <code>nums</code> by performing the following operations <strong>any</strong> number of times (including zero):</p>
<ul>
<li>Sele... | 1 | {
"code": "class Solution {\npublic:\n int minimumArrayLength(vector<int>& nums) {\n int n=nums.size();\n sort(nums.begin(),nums.end());\n int i=1,cnt=1,cnt2=0;\n while(i<n&&nums[i]==nums[i-1]){\n cnt++;\n i++;\n }\n for(int j=1;j<n;j++){\n ... |
3,244 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> containing <strong>positive</strong> integers.</p>
<p>Your task is to <strong>minimize</strong> the length of <code>nums</code> by performing the following operations <strong>any</strong> number of times (including zero):</p>
<ul>
<li>Sele... | 1 | {
"code": "class Solution\n{\npublic:\n int minimumArrayLength(vector<int> &nums)\n {\n sort(nums.begin(), nums.end());\n int el = nums[0];\n int minval = nums[0];\n int cnt = 0;\n int n = nums.size();\n\n for (int i = 0; i < n; i++)\n {\n if (nums[i] ... |
3,244 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> containing <strong>positive</strong> integers.</p>
<p>Your task is to <strong>minimize</strong> the length of <code>nums</code> by performing the following operations <strong>any</strong> number of times (including zero):</p>
<ul>
<li>Sele... | 1 | {
"code": "class Solution {\npublic:\n int minimumArrayLength(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n if(nums.size()<=2) return 1;\n int zero=0,hcf=nums[0];\n if(nums[0]!=nums[1]) return 1;\n \n for(auto it:nums) hcf=__gcd(hcf,it);\n \n for(au... |
3,244 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> containing <strong>positive</strong> integers.</p>
<p>Your task is to <strong>minimize</strong> the length of <code>nums</code> by performing the following operations <strong>any</strong> number of times (including zero):</p>
<ul>
<li>Sele... | 2 | {
"code": "class Solution\n{\npublic:\n int minimumArrayLength(vector<int> &nums){\n sort(nums.begin(), nums.end());\n int el = nums[0];\n int minval = nums[0];\n int cnt = 0;\n int n = nums.size();\n for (int i = 0; i < n; i++){\n if (nums[i] == el) cnt++;\n ... |
3,244 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> containing <strong>positive</strong> integers.</p>
<p>Your task is to <strong>minimize</strong> the length of <code>nums</code> by performing the following operations <strong>any</strong> number of times (including zero):</p>
<ul>
<li>Sele... | 2 | {
"code": "class Solution\n{\npublic:\n int minimumArrayLength(vector<int> &nums)\n {\n sort(nums.begin(), nums.end());\n int el = nums[0];\n int minval = nums[0];\n int cnt = 0;\n int n = nums.size();\n\n for (int i = 0; i < n; i++)\n {\n if (nums[i] ... |
3,244 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> containing <strong>positive</strong> integers.</p>
<p>Your task is to <strong>minimize</strong> the length of <code>nums</code> by performing the following operations <strong>any</strong> number of times (including zero):</p>
<ul>
<li>Sele... | 3 | {
"code": "class Solution\n{\npublic:\n int minimumArrayLength(vector<int> &nums)\n {\n sort(nums.begin(), nums.end());\n int el = nums[0];\n int minval = nums[0];\n int cnt = 0;\n int n = nums.size();\n\n for (int i = 0; i < n; i++)\n {\n if (nums[i] ... |
3,244 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> containing <strong>positive</strong> integers.</p>
<p>Your task is to <strong>minimize</strong> the length of <code>nums</code> by performing the following operations <strong>any</strong> number of times (including zero):</p>
<ul>
<li>Sele... | 3 | {
"code": "class Solution {\npublic:\n int minimumArrayLength(vector<int>& nums)\n {\n stack<int>st;\n int n=nums.size();\n int ans=n;\n sort(nums.begin(),nums.end());\n int equal=1,greater=0;\n int smallest=nums[0];\n\n for(int i=1;i<n;i++)\n {\n ... |
3,244 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> containing <strong>positive</strong> integers.</p>
<p>Your task is to <strong>minimize</strong> the length of <code>nums</code> by performing the following operations <strong>any</strong> number of times (including zero):</p>
<ul>
<li>Sele... | 3 | {
"code": "class Solution {\npublic:\n int minimumArrayLength(vector<int>& nums)\n {\n stack<int>st;\n int n=nums.size();\n int ans=n;\n sort(nums.begin(),nums.end());\n int equal=1,greater=0;\n int smallest=nums[0];\n\n for(int i=1;i<n;i++)\n {\n ... |
3,244 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> containing <strong>positive</strong> integers.</p>
<p>Your task is to <strong>minimize</strong> the length of <code>nums</code> by performing the following operations <strong>any</strong> number of times (including zero):</p>
<ul>
<li>Sele... | 3 | {
"code": "class Solution {\npublic:\n int minimumArrayLength(vector<int>& nums) {\n sort(nums.rbegin(),nums.rend());\n\n int n=nums.size();\n int last=nums[n-1];\n\n int count=0;\n for(auto x:nums){\n if(x!=last && x%last) return 1;\n if(x==last)count++;\n ... |
3,244 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> containing <strong>positive</strong> integers.</p>
<p>Your task is to <strong>minimize</strong> the length of <code>nums</code> by performing the following operations <strong>any</strong> number of times (including zero):</p>
<ul>
<li>Sele... | 3 | {
"code": "class Solution {\npublic:\n int minimumArrayLength(vector<int>& nums) {\n int n= nums.size();\n sort(nums.begin(), nums.end());\n unordered_map<int, int> hash;\n for(int i=0; i<n; i++){\n hash[nums[i]]++;\n }\n \n int num= nums[0];\n if(... |
3,244 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> containing <strong>positive</strong> integers.</p>
<p>Your task is to <strong>minimize</strong> the length of <code>nums</code> by performing the following operations <strong>any</strong> number of times (including zero):</p>
<ul>
<li>Sele... | 3 | {
"code": "class Solution {\npublic:\n int minimumArrayLength(vector<int>& nums) {\n unordered_map<int,int> mp;\n int mn=INT_MAX;\n for(auto i:nums){\n mp[i]++;\n mn=min(i,mn);\n }\n if(mp[mn]==1)\n return 1;\n for(auto [k,v]:mp)\n ... |
3,244 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> containing <strong>positive</strong> integers.</p>
<p>Your task is to <strong>minimize</strong> the length of <code>nums</code> by performing the following operations <strong>any</strong> number of times (including zero):</p>
<ul>
<li>Sele... | 3 | {
"code": "class Solution {\npublic:\n int minimumArrayLength(vector<int>& nums) {\n int n= nums.size();\n sort(nums.begin(), nums.end());\n unordered_map<int, int> hash;\n for(int i=0; i<n; i++){\n hash[nums[i]]++;\n }\n \n int num= nums[0];\n if(... |
3,244 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> containing <strong>positive</strong> integers.</p>
<p>Your task is to <strong>minimize</strong> the length of <code>nums</code> by performing the following operations <strong>any</strong> number of times (including zero):</p>
<ul>
<li>Sele... | 3 | {
"code": "class Solution {\npublic:\n int minimumArrayLength(vector<int>& nums) {\n int n= nums.size();\n // sort(nums.begin(), nums.end());\n unordered_map<int, int> hash;\n int num= INT_MAX;\n for(int i=0; i<n; i++){\n hash[nums[i]]++;\n num= min(num, num... |
3,244 | <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> containing <strong>positive</strong> integers.</p>
<p>Your task is to <strong>minimize</strong> the length of <code>nums</code> by performing the following operations <strong>any</strong> number of times (including zero):</p>
<ul>
<li>Sele... | 3 | {
"code": "class Solution {\npublic:\n int minimumArrayLength(vector<int>& nums) {\n map<int,int>m;\n for(auto i:nums)\n m[i]++;\n int cnt=0;\n int n=m.begin()->first;\n int it=m.begin()->second;\n for(auto i:nums)\n {\n if(i!=n and i%n!=0)\n ... |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1... | 0 | {
"code": "class Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n priority_queue<pair<int,int>> k_queue;\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> else_queue;\n long long sum = 0;\n long long min = 0;\n vecto... |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1... | 0 | {
"code": "class Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n int n = nums.size();\n // [36,28,42,36,39,13,24,3,32,16,11,43,21,40,34,49,29,20,34,34,8,3,41,6,46,5,35,5,47,2]\n // 25,26\n // max,min\n priority_queue<pair<int,int>>p1;\n ... |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1... | 0 | {
"code": "class Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n return byPQ(nums, k, dist);\n\n // return byMultiset(nums, k, dist);\n }\n\n long long byPQ(vector<int>& nums, int k, int dist) {\n int n = nums.size();\n k--; //第一个数不用计算\n\n ... |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1... | 0 | {
"code": "class Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n return byPQ(nums, k, dist);\n\n // return byMultiset(nums, k, dist);\n }\n\n long long byPQ(vector<int>& nums, int k, int dist) {\n int n = nums.size();\n k--;\n\n long long su... |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1... | 0 | {
"code": "class Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n return byPQ(nums, k, dist);\n\n // return byMultiset(nums, k, dist);\n }\n\n long long byPQ(vector<int>& nums, int k, int dist) {\n int n = nums.size();\n k--;\n\n long long su... |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1... | 0 | {
"code": "class Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n return byPQ(nums, k, dist);\n\n // return byMultiset(nums, k, dist);\n }\n\n long long byPQ(vector<int>& nums, int k, int dist) {\n int n = nums.size();\n k--;\n\n long long... |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1... | 0 | {
"code": "class Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n return byPQ(nums, k, dist);\n\n // return byMultiset(nums, k, dist);\n }\n\n long long byPQ(vector<int>& nums, int k, int dist) {\n int n = nums.size();\n k--;\n\n long long... |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1... | 0 | {
"code": "class SegmentTree{\nprivate:\n const int N;\n const vector<int>& VALUES;\n vector<long long> sum;\n vector<long long> count;\n\n void add(int node, int l, int r, int pos, int countVal){\n if(l == r){\n count[node] += countVal;\n sum[node] = count[node] * VALUES[r... |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1... | 0 | {
"code": "class Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n int n = nums.size();\n multiset<int> s(nums.begin()+1,nums.begin()+dist+2);\n \n // 'sum' will represent the sum of smallest k-1 element in a window.\n long long sum = 0;\n ... |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1... | 0 | {
"code": "class Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n int n = nums.size();\n multiset<int> s(nums.begin()+1,nums.begin()+dist+2);\n \n // 'sum' will represent the sum of smallest k-1 element in a window.\n long long sum = 0;\n ... |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1... | 0 | {
"code": "class Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n int n=nums.size();\n multiset<int>minh;\n multiset<int,greater<int>>maxh;\n // Store the smallest k elements in maxh and the remaining ones in minh\n long long sum=0;\n vec... |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1... | 0 | {
"code": "#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\ntemplate<typename dt>\nusing ordered_set = tree<dt, null_type, less<dt>, rb_tree_tag, tree_order_statistics_node_update>;\nusing pii = pair<int, int>;\n#define IFOR(i, a, n) for(int i=a; i<n; i++)\n... |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1... | 0 | {
"code": "#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\nusing namespace __gnu_pbds;\ntemplate<typename dt>\nusing ordered_set = tree<dt, null_type, less<dt>, rb_tree_tag, tree_order_statistics_node_update>;\nusing pii = pair<int, int>;\n#define IFOR(i, a, n) for(int i=a; i<n; i++)\n... |
3,260 | <p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and two <strong>positive</strong> integers <code>k</code> and <code>dist</code>.</p>
<p>The <strong>cost</strong> of an array is the value of its <strong>first</strong> element. For example, the cost of <code>[1... | 0 | {
"code": "class Solution {\npublic:\n long long minimumCost(vector<int>& nums, int k, int dist) {\n //The problem can be simplified into finding the minimum of the sum of the smallest \n //(k−1)th elements in a continuous subarray of length dist+1.\n long long ans = 1e15;\n\n int i = 1... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.