id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 0 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n int n=nums.size();\n if(n<2) return 0;\n sort(nums.begin(),nums.end());\n int max1=0;\n for(int i=1;i<n;i++){\n max1=max((nums[i]-nums[i-1]),max1);\n }\n return max1; \n }\n}... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 2 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n int n=nums.size();\n int arr[n];\n for(int i=0 ; i<nums.size() ; i++){\n arr[i]=nums[i];\n }\n sort(arr,arr+n);\n int c=0;\n for(int i=1 ; i<n ; i++){\n if(arr[i]-arr[... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 2 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n int n=nums.size();\n sort(nums.rbegin(),nums.rend());\n int max1=0;\n for(int i=0;i<n-1;i++)\n {\n if(n<2)\n {\n return 0;\n }\n int max=nums[i]-nums[i+1];\n if(ma... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 2 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n int n=nums.size();\n sort(nums.rbegin(),nums.rend());\n int max1=0;\n for(int i=0;i<n-1;i++)\n {\n if(n<2)\n {\n return 0;\n }\n int max=nums[i]-nums[i+1];\n if(ma... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 2 | {
"code": "class Solution {\npublic:\n void radixSort(const int& n, vector<int>& nums, vector<int>& output, int k) {\n int count[10] = {0};\n for (auto& x : nums) {\n count[(x/k) % 10]++;\n }\n for (int i=1; i<10; i++) {\n count[i] += count[i-1];\n }\n ... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 2 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n int size = nums.size();\n if(size < 2)\n return 0;\n int max_el = nums[0];\n for(int i = 1; i < size; ++i)\n {\n if(nums[i] > max_el)\n max_el = nums[i];\n }\n ... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 2 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n int size = nums.size();\n if(size < 2)\n return 0;\n int max_el = nums[0];\n for(int i = 1; i < size; ++i)\n {\n if(nums[i] > max_el)\n max_el = nums[i];\n }\n ... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 2 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n int n=nums.size();\n if(n==1)return 0;\n sort(nums.begin(),nums.end());\n map<int,int>mp;\n for(int i=1;i<n;i++){\n mp[nums[i]-nums[i-1]]++;\n }\n\n int maxi=0;\n\n for(au... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 2 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n if(nums.size() < 2){\n return 0;\n }\n map<int , int>mp;\n int maxDiff = INT_MIN;\n sort(nums.begin() , nums.end());\n for(int i = 1 ; i < nums.size() ; i++){\n int diff = n... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 2 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n if(nums.size()<2){\n return 0;\n }\n priority_queue<int> pq(nums.begin(),nums.end());\n int maxi = INT_MIN;\n while(!pq.empty()){\n int ele = pq.top();\n pq.pop();\n ... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 2 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n if (nums.size() < 2) return 0;\n \n vector<int> sorted_nums = nums;\n sort(sorted_nums.begin(), sorted_nums.end());\n \n int max_diff = 0;\n for (size_t i = 1; i < sorted_nums.size(); ++i) ... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 2 | {
"code": "inline const auto optimize = []() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n\nclass Solution {\npublic:\n static int maximumGap(const std::vector<int>& nums) {\n const int N = nums.size();\n\n // This is technica... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 2 | {
"code": "class Solution {\npublic:\n int maximumGap(const std::vector<int>& nums) {\n const int N = nums.size();\n if (N < 2) {\n return 0;\n }\n \n // This is technically N*log(N), but let's see if it even works?\n auto [mmin, mmax] = std::ranges::minmax(nums... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 2 | {
"code": "inline const auto optimize = []() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n\nclass Solution {\npublic:\n static int maximumGap(const std::vector<int>& nums) {\n const int N = nums.size();\n if (N < 2) {\n ret... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 2 | {
"code": "// time and space: O(N)가 되게 하라. (sorting을 하면 안됨.)\nclass Solution {\npublic:\nint maximumGap(vector<int>& nums) {\n\tif (nums.size() < 2) return 0;\n\tint minNum, maxNum, N=nums.size();\n\tminNum = maxNum = nums[0];\n\tfor (int i=1; i<N; i++) {\n\t\tminNum = min(minNum, nums[i]);\n\t\tmaxNum = max(maxNum, ... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 2 | {
"code": "// time and space: O(N)가 되게 하라. (sorting을 하면 안됨.)\nclass Solution {\npublic:\nint maximumGap(vector<int>& nums) {\n\tif (nums.size() < 2) return 0;\n\tint minNum, maxNum, N=nums.size();\n\tminNum = maxNum = nums[0];\n\tfor (int i=1; i<N; i++) {\n\t\tminNum = min(minNum, nums[i]);\n\t\tmaxNum = max(maxNum, ... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int getBucketId(int num, int minNum, int bucketSize){\n int x = ( num - minNum ) / bucketSize;\n cout << \" idx get: \" << x << \" val: \" << num << endl;\n return x;\n }\n\n int maximumGap(vector<int>& nums) {\n int minNum = INT_MAX;\n ... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int getBucketId(int num, int minNum, int bucketSize){\n // int x = ( num - minNum ) / bucketSize;\n // cout << \" idx get: \" << x << \" val: \" << num << endl;\n return ( num - minNum ) / bucketSize;\n }\n\n int maximumGap(vector<int>& nums) {\n ... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n const int size = nums.size();\n const auto [minimum, maximum] = std::minmax_element(nums.begin(), nums.end());\n if (*minimum == *maximum) {\n return 0;\n }\n const int bucketSize = std::max(... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n int n = nums.size();\n if (n < 2) return 0;\n\n int minElem = *min_element(nums.begin(), nums.end());\n int maxElem = *max_element(nums.begin(), nums.end());\n\n if (minElem == maxElem) return 0;\n\n ... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n int n=nums.size();\n if(n<2) return 0;\n priority_queue<int,vector<int>,greater<int>> q;\n for(auto it:nums){\n q.push(it);\n }\n int ele=q.top();\n q.pop();\n int maxi=IN... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n vector<int> v;\n int i;\n if(nums.size()==1) return 0;\n sort(nums.begin(),nums.end());\n for(i=1;i<nums.size();i++)\n {\n v.push_back(nums[i]-nums[i-1]);\n }\n return *ma... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "#include <tuple>\n#include <vector>\nusing namespace std;\n\nclass Solution {\npublic:\n pair<int, int> findMaxAndMin(vector<int> values) {\n int maxValue = values[0];\n int minValue = values[0];\n\n for (int i = 0; i < values.size(); i++) {\n if (values[i] > maxValue) {\... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n if (nums.size() <= 1)\n return 0;\n \n // Find maximum\n int max = *std::max_element(nums.cbegin(), nums.cend());\n int n_digits = (max == 0) ? 1 : 0;\n while (max > 0) {\n m... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n unordered_map<int,int> C;\n int power(int n,int i){\n if(i==0){\n return 1;\n }\n if(C.count(i)==1){\n return C[i];\n }\n C[i]=n*power(n,i-1);\n return C[i];\n }\n void radix_sort(vector<int>& nums){\n ... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n unordered_map<int,int> C;\n int power(int n,int i){\n if(i==0){\n return 1;\n }\n if(C.count(i)==1){\n return C[i];\n }\n C[i]=n*power(n,i-1);\n return C[i];\n }\n void radix_sort(vector<int>& nums){\n ... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "#include <tuple>\nclass Solution {\npublic:\n pair<int, int> find_max_min(vector<int> input) {\n int max = input[0], min = input[0];\n\n for (int i = 0; i < input.size(); i++) {\n if (input[i] > max) {\n max = input[i];\n }\n\n if (input[i] <... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "#include <tuple>\n#include <vector>\nusing namespace std;\n\nclass Solution {\npublic:\n pair<int, int> findMaxAndMin(vector<int> values) {\n int maxValue = values[0];\n int minValue = values[0];\n\n for (int i = 0; i < values.size(); i++) {\n if (values[i] > maxValue) {\... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n \n int getDigit(int num, int factor) \n {\n return (abs(num) / abs(factor)) % 10;\n }\n \n void radixCountingSort(vector<int> &nums, int factor) \n {\n int freqSize = 10, size = nums.size();\n vector<int> freq(freqSize, 0), sorted(size, ... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n if (nums.size() < 2) return 0;\n int max_num = *max_element(nums.begin(), nums.end());\n int exp = 1;\n vector<int> aux(nums.size());\n vector<vector<int>> buckets(10);\n while (max_num / exp > 0)... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "#include <ranges>\n\nclass Solution\n{\n public:\n int maximumGap(std::vector<int>& nums) const\n {\n if(nums.size()<2) return 0;\n \n std::array<std::vector<int>,10> buckets;\n\n for(unsigned mod = 1; mod<=1000000000u; mod*=10)\n {\n for(auto v: nums)... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n\n void bucketSort(vector<int>& nums) {\n int base = 10;\n int d = 1;\n vector<vector<int>> buckets(base);\n int N = nums.size();\n while (true){\n for (int i = 0; i < N; ++i){\n int e = nums[i];\n int... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n\n int getIthDigit(int i, int num){\n for(int j=0;j<i;++j) num=num/10;\n return num%10;\n }\n\n int maximumGap(vector<int>& nums) {\n if(nums.size()<2) return 0;\n vector<vector<int>> bucket;\n for(int i=0;i<10;++i) bucket.push_back(vec... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums)\n {\n if(nums.size()<2)\n return 0;\n\n vector<int> temp; // ya to array ka size initialize mt karo ya fr push_back mt use kr\n priority_queue<int,vector<int>,greater<int>> q;\n\n for(int i=0;i<nums... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n void countsort(vector<int> &nums,int place){\n vector<int>temp(10,0);\n vector<int>res(nums.size());\n for(int i=0;i<nums.size();i++){\n temp[(nums[i]/place)%10]++;\n }\n for(int i=1;i<temp.size();i++){\n temp[i]+=temp[... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n if (nums.size() < 2) return 0;\n radixSort(nums);\n \n int max_gap = 0;\n for (size_t i = 1; i < nums.size(); ++i) {\n max_gap = max(max_gap, nums[i] - nums[i - 1]);\n }\n \n ... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n void countsort(vector<int> &nums,int place){\n vector<int>temp(10,0);\n vector<int>res(nums.size());\n for(int i=0;i<nums.size();i++){\n temp[(nums[i]/place)%10]++;\n }\n for(int i=1;i<temp.size();i++){\n temp[i]+=temp[... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int pos(int x, int fact) {\n return ((int)(x / pow(10, fact)) % 10);\n }\n\n void countSort(vector<int>& nums, int fact) {\n int N = (int)nums.size();\n vector<int> count(10, 0);\n for (int i : nums) \n count[pos(i, fact)]++;\n ... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n\n int findBucket(int num, int maxnum, int minnum, int n){\n int d=(maxnum-minnum+1)/(n-1);\n int nlarge=0;\n nlarge=(maxnum-minnum+1)%(n-1);\n // cout<<\"d=\"<<d<<\"\\n\";\n // cout<<\"nlarge=\"<<nlarge<<\"\\n\";\n int gap=num-minnum;... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n vector<int> bucket[1024];\n void radix_sort(vector<int>& nums){\n int x;\n //round 1\n for(int i{};i<nums.size();++i){\n bucket[nums[i]&1023].push_back(nums[i]);\n }\n x=0;\n for(auto &b : bucket){\n copy(b.be... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n vector<int> bucket[1024];\n void radix_sort(vector<int>& nums){\n int x;\n //round 1\n for(int i{};i<nums.size();++i){\n bucket[nums[i]&1023].push_back(nums[i]);\n }\n x=0;\n for(auto &b : bucket){\n copy(b.be... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n vector<int> bucket[1024];\n void radix_sort(vector<int>& nums){\n int x;\n //round 1\n for(int i{};i<nums.size();++i){\n bucket[nums[i]&1023].push_back(nums[i]);\n }\n x=0;\n for(auto &b : bucket){\n copy(b.be... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n void radixsort(vector<int>& nums){\n int exp = 1;\n for(int i=0 ; i<9; i++){\n countSort(nums,exp);\n exp*=10;\n }\n }\n void countSort(vector<int>& nums, int exp){\n\n int count[10] = {0};\n for(int i=0 ; i<nums.... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n \n int maxi=*max_element(nums.begin(),nums.end());\n int mini=*min_element(nums.begin(),nums.end());\n\n int n=nums.size();\n if(n==1)return 0;\n int sz=(maxi-mini)/(n-1);\n if(sz*(n-1)!=ma... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n int b_min = INT_MAX;\n int b_max = INT_MIN;\n int i, index, pre;\n int ** bucket;\n int gap, max_gap=-1;\n if (nums.size() == 1) {\n return 0;\n }\n if (nums.size() == 2) ... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n int n = nums.size();\n for (long i=1; i<=1000000000; i*=10) {\n vector<int> output(n);\n countSort(nums, i, output);\n nums = output;\n }\n \n int mx = 0;\n for (i... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n int n = nums.size();\n for (long i=1; i<=1000000000; i*=10) {\n vector<int> output(n);\n countSort(nums, i, output);\n nums = output;\n }\n \n int mx = 0;\n for (i... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int n;\n void bucketsort(vector<int> & nums,long long int dec){\n vector<int> count(10);\n for(auto &i: nums){\n int t=(i/dec)%10;\n // cout<<t<<' ';\n count[t]++;\n }\n for(int i=1;i<10;i++){\n count[... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n for (unsigned bit=0; bit<32; ++bit) {\n stable_partition(nums.begin(), nums.end(), [bit](int a) {\n return bit==31 ? (a&(1<<bit)) : !(a&(1<<bit)); \n }); \n }\n int diff = 0; \n for (int i=1; i<n... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n for (char bit=0; bit<32; ++bit) {\n stable_partition(nums.begin(), nums.end(), [bit](int a) {\n return bit==31 ? (a&(1<<bit)) : !(a&(1<<bit)); \n }); \n }\n int diff = 0; \n for (int i=1; i<nums.... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "// if(nums.size()<2) return 0;\n // sort(nums.begin(),nums.end());\n // int ans = INT_MIN;\n // for(int i = 1; i < nums.size(); i++){\n // ans = max(ans,nums[i]-nums[i-1]);\n // }\n // return ans;\n\nclass Solution {\npublic:\n int maximumGap(vector<int>& nu... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "/*\nRef1. https://leetcode.com/problems/maximum-gap/\nRef2. https://www.youtube.com/watch?v=gsNsgAY-WhE\n*/\nclass Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n const int n = nums.size();\n if (n == 1) {\n return 0;\n }\n\n int min_val = nums[0];\n ... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "/*\nRef1. https://leetcode.com/problems/maximum-gap/\nRef2. https://www.youtube.com/watch?v=gsNsgAY-WhE\n*/\nclass Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n const int n = nums.size();\n if (n == 1) {\n return 0;\n }\n\n int min_val = nums[0];\n ... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n int n = nums.size();\n if (n < 2) return 0;\n \n int mmin = *min_element(nums.begin(), nums.end());\n int mmax = *max_element(nums.begin(), nums.end());\n \n int bucketSize = max(1, (mmax -... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n int n = nums.size();\n if (n < 2) return 0;\n \n int mmin = *min_element(nums.begin(), nums.end());\n int mmax = *max_element(nums.begin(), nums.end());\n \n int bucketSize = max(1, (mmax -... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n\n for (int i = 0; i < 4; i++) {\n\n sortByByte(i, nums);\n\n }\n\n int ans = 0;\n\n for (int i = 1; i < nums.size(); i++) {\n ans = max(ans, nums[i] - nums[i - 1]);\n }\n\n r... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int getChar(int num, long k) // 1941 - k = 100 -> get 4\n {\n num -= num % (k / 10);\n num = num % (k);\n num = num / (k / 10);\n return num;\n }\n int maximumGap(vector<int>& nums) {\n int d = 10;\n int n = nums.size();\n ... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& arr) {\n int n = arr.size();\n\n set<int> st(begin(arr), end(arr));\n\n int prev = *st.begin();\n st.erase(prev);\n\n int mx = 0;\n\n while (!st.empty()) {\n int cur = *st.begin();\n s... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n int n=nums.size();\n if(n<2)return 0;\n set<int> st;\n for(int i=0;i<n;i++){\n st.insert(nums[i]);\n }\n int cnt=0;\n int prev=-1;\n int i=0;\n for(auto it:st){\n ... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n if(nums.size()<2) return 0;\n int last=*min_element(nums.begin(),nums.end());\n int gap=0;\n set<int> s;\n for(auto x:nums) s.insert(x);\n for(auto it=s.begin();it!=s.end();it++){\n gap... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n if(nums.size()<2) return 0;\n int last=*min_element(nums.begin(),nums.end());\n int gap=0;\n set<int> s;\n for(auto x:nums) s.insert(x);\n for(auto it=s.begin();it!=s.end();it++){\n gap... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n set<int> ns (nums.begin(), nums.end());\n if (ns.size() < 2) return 0;\n int prev = *ns.begin();\n int del = 0;\n for(int n : ns) {\n del = max(del, n-prev);\n prev = n;\n }\... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n set<int> setOfElements;\n if(nums.size() < 2) return 0;\n for(int i=0;i<nums.size();i++) {\n setOfElements.insert(nums[i]);\n }\n\n int flag = 0;\n int element;\n int maxGap = 0;... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n int n=nums.size();\n if(n<2) return 0;\n\n int minNum=*min_element(nums.begin(),nums.end());\n int maxNum=*max_element(nums.begin(),nums.end());\n\n int bucketSize=max(1,(maxNum-minNum)/(n-1));\n\n ... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n if (nums.size() < 2) return 0; \n\n int n = nums.size();\n\n map<int, int> freq;\n for (auto it : nums) {\n freq[it]++;\n }\n\n nums.clear();\n for(auto it : freq) {\n ... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n int n = nums.size();\n\n if(n < 2){\n return 0;\n }\n\n map<int,int> m;\n\n for(auto num: nums){\n m[num]++;\n }\n\n int ans = 0;\n int prev = -1;\n\n fo... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n if(nums.size()==1)\n return 0;\n map<int,int>mp;\n for(auto &it:nums)\n mp[it]++;\n\n int ans=0;\n auto it=mp.begin();\n it++;\n auto prev=mp.begin();\n for(;it... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n int maximumGap(vector<int>& nums) {\n if(nums.size()==1)\n return 0;\n map<int ,int>mp;\n for(int i=0;i<nums.size();i++)\n {\n mp[nums[i]]=i;\n }\n int dif=0,count=0;\n int n1=0,n2=0;\n for(auto it:mp)\... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n vector<int> countsort(vector<int>& nums) {\n int n = nums.size();\n map<int, int> m;\n for (int i = 0; i < n; i++)\n m[nums[i]]++;\n\n nums.clear();\n for (auto it = m.begin(); it != m.end(); it++) {\n int cnt = it->sec... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "class Solution {\npublic:\n vector<int> countsort(vector<int> &nums)\n {\n int n = nums.size();\n map<int, int> m;\n for (int i = 0; i < n; i++ )\n m[nums[i]]++;\n\n nums.clear();\n for (auto it : m)\n {\n int cnt = it.second;\n ... |
164 | <p>Given an integer array <code>nums</code>, return <em>the maximum difference between two successive elements in its sorted form</em>. If the array contains less than two elements, return <code>0</code>.</p>
<p>You must write an algorithm that runs in linear time and uses linear extra space.</p>
<p> </p>
<p><st... | 3 | {
"code": "\nclass Solution {\npublic:\n\nvector<int> countsort(vector<int> &nums)\n{\n int n = nums.size();\n map<int, int> m;\n for (int i = 0; i < n; i++)\n m[nums[i]]++;\n\n nums.clear();\n for (auto it : m)\n {\n int cnt = it.second;\n while (cnt--)\n {\n ... |
1,626 | <p>A sequence of numbers is called an <strong>arithmetic progression</strong> if the difference between any two consecutive elements is the same.</p>
<p>Given an array of numbers <code>arr</code>, return <code>true</code> <em>if the array can be rearranged to form an <strong>arithmetic progression</strong>. Otherwise,... | 0 | {
"code": "class Solution {\npublic:\n bool canMakeArithmeticProgression(vector<int>& arr) {\n if(arr.size()==1||arr.size()==2)return true;\n sort(arr.begin(),arr.end());\nfor(int i=0;i<arr.size();i++){\n cout<<arr[i]<<\" \";\n}\n int a=abs(arr[1]-arr[0]);\n for(int i=1;i<arr.size()-... |
1,626 | <p>A sequence of numbers is called an <strong>arithmetic progression</strong> if the difference between any two consecutive elements is the same.</p>
<p>Given an array of numbers <code>arr</code>, return <code>true</code> <em>if the array can be rearranged to form an <strong>arithmetic progression</strong>. Otherwise,... | 0 | {
"code": "class Solution {\npublic:\n bool canMakeArithmeticProgression(vector<int>& arr) {\n if(arr.size() == 2)\n return true;\n \n sort(arr.begin(), arr.end());\n int d = arr[1] - arr[0];\n\n for(int i = 1; i < arr.size() - 1; i++) {\n if(arr[i+1] - arr[... |
1,626 | <p>A sequence of numbers is called an <strong>arithmetic progression</strong> if the difference between any two consecutive elements is the same.</p>
<p>Given an array of numbers <code>arr</code>, return <code>true</code> <em>if the array can be rearranged to form an <strong>arithmetic progression</strong>. Otherwise,... | 0 | {
"code": "class Solution {\npublic:\n bool canMakeArithmeticProgression(vector<int>& arr) {\n sort(arr.begin(),arr.end());\n int d = arr[1]-arr[0];\n for(int i = 1 ; i < arr.size()-1;i++){\n if(arr[i+1] - arr[i] != d) return false;\n }\n return true;\n }\n};",
"m... |
1,626 | <p>A sequence of numbers is called an <strong>arithmetic progression</strong> if the difference between any two consecutive elements is the same.</p>
<p>Given an array of numbers <code>arr</code>, return <code>true</code> <em>if the array can be rearranged to form an <strong>arithmetic progression</strong>. Otherwise,... | 0 | {
"code": "class Solution {\npublic:\n bool canMakeArithmeticProgression(vector<int>& arr) {\n int n=arr.size();\n\n int a=*min_element(arr.begin(),arr.end());\n int an=*max_element(arr.begin(),arr.end());\n \n\n if((an-a)%(n-1)!=0) return false;\n int d=(an-a)/(n-1);\n ... |
1,626 | <p>A sequence of numbers is called an <strong>arithmetic progression</strong> if the difference between any two consecutive elements is the same.</p>
<p>Given an array of numbers <code>arr</code>, return <code>true</code> <em>if the array can be rearranged to form an <strong>arithmetic progression</strong>. Otherwise,... | 0 | {
"code": "class Solution {\npublic:\n bool canMakeArithmeticProgression(vector<int>& arr) {\n sort(arr.begin(),arr.end());\n for(int i=1;i<arr.size()-1;i++) {if(arr[i+1]+arr[i-1]!=2*arr[i]) return false;}\n return true;\n }\n};",
"memory": "11800"
} |
1,626 | <p>A sequence of numbers is called an <strong>arithmetic progression</strong> if the difference between any two consecutive elements is the same.</p>
<p>Given an array of numbers <code>arr</code>, return <code>true</code> <em>if the array can be rearranged to form an <strong>arithmetic progression</strong>. Otherwise,... | 1 | {
"code": "class Solution {\npublic:\n bool canMakeArithmeticProgression(vector<int>& arr) {\n sort(arr.begin(), arr.end()) ;\n int d = arr.at(1)-arr.at(0) ;\n for(int i=0; i<arr.size()-1; ++i)\n if((arr.at(i+1)-arr.at(i)) != d)\n return false ;\n return true ;... |
1,626 | <p>A sequence of numbers is called an <strong>arithmetic progression</strong> if the difference between any two consecutive elements is the same.</p>
<p>Given an array of numbers <code>arr</code>, return <code>true</code> <em>if the array can be rearranged to form an <strong>arithmetic progression</strong>. Otherwise,... | 1 | {
"code": "class Solution {\npublic:\n bool canMakeArithmeticProgression(vector<int>& arr) {\n sort(arr.begin(), arr.end()) ;\n int d = arr.at(1)-arr.at(0) ;\n for(int i=0; i<arr.size()-1; ++i)\n if((arr.at(i+1)-arr.at(i)) != d)\n return false ;\n return true ;... |
1,626 | <p>A sequence of numbers is called an <strong>arithmetic progression</strong> if the difference between any two consecutive elements is the same.</p>
<p>Given an array of numbers <code>arr</code>, return <code>true</code> <em>if the array can be rearranged to form an <strong>arithmetic progression</strong>. Otherwise,... | 3 | {
"code": "#include <limits>\n\nclass Solution {\npublic:\n bool canMakeArithmeticProgression(vector<int>& arr) {\n int max_v = std::numeric_limits<int>::min();\n int min_v = std::numeric_limits<int>::max();\n\n for (auto val:arr) {\n if (val>max_v) {\n max_v=val;\n ... |
1,626 | <p>A sequence of numbers is called an <strong>arithmetic progression</strong> if the difference between any two consecutive elements is the same.</p>
<p>Given an array of numbers <code>arr</code>, return <code>true</code> <em>if the array can be rearranged to form an <strong>arithmetic progression</strong>. Otherwise,... | 3 | {
"code": "class Solution {\npublic:\n bool canMakeArithmeticProgression(vector<int>& arr) \n {\n sort( arr.begin(), arr.end() );\n\n int razlika = arr[1] - arr[0];\n\n for( int i = 2; i < arr.size(); i++ )\n {\n if( arr[i] - arr[i-1] != razlika ) return false;\n } ... |
1,626 | <p>A sequence of numbers is called an <strong>arithmetic progression</strong> if the difference between any two consecutive elements is the same.</p>
<p>Given an array of numbers <code>arr</code>, return <code>true</code> <em>if the array can be rearranged to form an <strong>arithmetic progression</strong>. Otherwise,... | 3 | {
"code": "class Solution {\npublic:\n bool canMakeArithmeticProgression(vector<int>& arr) {\n sort(arr.begin(),arr.end());\n set<int> s;\n for(int i=0;i<arr.size()-1;i++){\n s.insert(arr[i+1]-arr[i]);\n }\n if(s.size()>1) return false;\n return true... |
1,626 | <p>A sequence of numbers is called an <strong>arithmetic progression</strong> if the difference between any two consecutive elements is the same.</p>
<p>Given an array of numbers <code>arr</code>, return <code>true</code> <em>if the array can be rearranged to form an <strong>arithmetic progression</strong>. Otherwise,... | 3 | {
"code": "#include <algorithm>\n\nclass Solution {\npublic:\n bool canMakeArithmeticProgression(vector<int>& arr) {\n vector<int> sorted_arr(arr);\n std::sort(sorted_arr.begin(), sorted_arr.end());\n int difference = sorted_arr[1] - sorted_arr[0];\n for(size_t i = 1; i < sorted_arr.siz... |
1,626 | <p>A sequence of numbers is called an <strong>arithmetic progression</strong> if the difference between any two consecutive elements is the same.</p>
<p>Given an array of numbers <code>arr</code>, return <code>true</code> <em>if the array can be rearranged to form an <strong>arithmetic progression</strong>. Otherwise,... | 3 | {
"code": "class Solution {\n public:\n bool canMakeArithmeticProgression(vector<int>& arr) {\n const int n = arr.size();\n const int mx = ranges::max(arr);\n const int mn = ranges::min(arr);\n const int range = mx - mn;\n if (range % (n - 1) != 0)\n return false;\n const int diff = range / (n... |
1,626 | <p>A sequence of numbers is called an <strong>arithmetic progression</strong> if the difference between any two consecutive elements is the same.</p>
<p>Given an array of numbers <code>arr</code>, return <code>true</code> <em>if the array can be rearranged to form an <strong>arithmetic progression</strong>. Otherwise,... | 3 | {
"code": "#include <algorithm>\n#include <cmath>\n#include <unordered_set>\nclass Solution {\npublic:\n bool canMakeArithmeticProgression(vector<int>& arr) {\n int max = *std::max_element(arr.begin(), arr.end());\n int min = *std::min_element(arr.begin(), arr.end());\n\n size_t arr_len = arr.... |
1,627 | <p>We have a wooden plank of the length <code>n</code> <strong>units</strong>. Some ants are walking on the plank, each ant moves with a speed of <strong>1 unit per second</strong>. Some of the ants move to the <strong>left</strong>, the other move to the <strong>right</strong>.</p>
<p>When two ants moving in two <str... | 0 | {
"code": "class Solution {\npublic:\n int getLastMoment(int n, vector<int>& left, vector<int>& right) {\n int time=INT_MIN;\n for(int i=0;i<left.size();i++){\n time=max(time,(left[i]));\n }\n for(int i=0;i<right.size();i++){\n time=max(time,(n-right[i]));\n ... |
1,627 | <p>We have a wooden plank of the length <code>n</code> <strong>units</strong>. Some ants are walking on the plank, each ant moves with a speed of <strong>1 unit per second</strong>. Some of the ants move to the <strong>left</strong>, the other move to the <strong>right</strong>.</p>
<p>When two ants moving in two <str... | 0 | {
"code": "class Solution {\npublic:\n int getLastMoment(int n, vector<int>& left, vector<int>& right) {\n int ans = 0 ; \n int n1 = left.size();\n int n2 = right.size();\n int x = INT_MIN;\n if(n1>0){\n ans = *max_element(left.begin(), left.end()) ;\n }\n ... |
1,627 | <p>We have a wooden plank of the length <code>n</code> <strong>units</strong>. Some ants are walking on the plank, each ant moves with a speed of <strong>1 unit per second</strong>. Some of the ants move to the <strong>left</strong>, the other move to the <strong>right</strong>.</p>
<p>When two ants moving in two <str... | 0 | {
"code": "class Solution {\npublic:\n int getLastMoment(int n, vector<int>& left, vector<int>& right) {\n for(auto &r: right) {\n r = n-r;\n }\n int a = left.size() ? *max_element(left.begin(), left.end()) : 0;\n int b = right.size() ? *max_element(right.begin(), right.end()... |
1,627 | <p>We have a wooden plank of the length <code>n</code> <strong>units</strong>. Some ants are walking on the plank, each ant moves with a speed of <strong>1 unit per second</strong>. Some of the ants move to the <strong>left</strong>, the other move to the <strong>right</strong>.</p>
<p>When two ants moving in two <str... | 0 | {
"code": "class Solution {\npublic:\n int getLastMoment(int n, vector<int>& left, vector<int>& right) {\n int ans = 0;\n for(int i:left) ans = max(ans, i);\n for(int i:right) ans = max(ans, n-i);\n return ans;\n }\n};",
"memory": "25700"
} |
1,627 | <p>We have a wooden plank of the length <code>n</code> <strong>units</strong>. Some ants are walking on the plank, each ant moves with a speed of <strong>1 unit per second</strong>. Some of the ants move to the <strong>left</strong>, the other move to the <strong>right</strong>.</p>
<p>When two ants moving in two <str... | 0 | {
"code": "class Solution {\npublic:\n int getLastMoment(int n, vector<int>& left, vector<int>& right) {\n int ans = 0;\n for(int i:left) ans = max(ans, i);\n for(int i:right) ans = max(ans, n-i);\n return ans;\n }\n};",
"memory": "25700"
} |
1,627 | <p>We have a wooden plank of the length <code>n</code> <strong>units</strong>. Some ants are walking on the plank, each ant moves with a speed of <strong>1 unit per second</strong>. Some of the ants move to the <strong>left</strong>, the other move to the <strong>right</strong>.</p>
<p>When two ants moving in two <str... | 2 | {
"code": "class Solution {\npublic:\n int getLastMoment(int n, vector<int>& left, vector<int>& right) {\n int ans =0;\n for(auto x:left)\n ans = max(ans, x);\n for(auto x:right)\n ans = max(ans, n-x);\n return ans; \n }\n};",
"memory": "25800"
} |
1,627 | <p>We have a wooden plank of the length <code>n</code> <strong>units</strong>. Some ants are walking on the plank, each ant moves with a speed of <strong>1 unit per second</strong>. Some of the ants move to the <strong>left</strong>, the other move to the <strong>right</strong>.</p>
<p>When two ants moving in two <str... | 2 | {
"code": "class Solution {\npublic:\n int getLastMoment(int n, vector<int>& left, vector<int>& right) {\n left.push_back(0);\n right.push_back(n);\n return max(*max_element(left.begin(), left.end()), n - *min_element(right.begin(), right.end()));\n }\n};",
"memory": "25800"
} |
1,627 | <p>We have a wooden plank of the length <code>n</code> <strong>units</strong>. Some ants are walking on the plank, each ant moves with a speed of <strong>1 unit per second</strong>. Some of the ants move to the <strong>left</strong>, the other move to the <strong>right</strong>.</p>
<p>When two ants moving in two <str... | 3 | {
"code": "class Solution {\npublic:\n int getLastMoment(int n, vector<int>& left, vector<int>& right) {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n\n int lastMoment = 0;\n\n for(auto x : left) \n lastMoment = max(lastMoment,x);\n \n ... |
1,627 | <p>We have a wooden plank of the length <code>n</code> <strong>units</strong>. Some ants are walking on the plank, each ant moves with a speed of <strong>1 unit per second</strong>. Some of the ants move to the <strong>left</strong>, the other move to the <strong>right</strong>.</p>
<p>When two ants moving in two <str... | 3 | {
"code": "class Solution {\npublic:\n int getLastMoment(int n, vector<int>& left, vector<int>& right) {\n sort(left.begin(),left.end());\n sort(right.begin(),right.end());\n if(left.size()==0) return abs(right[0]-n);\n if(right.size()==0) return left[left.size()-1];\n // if(left... |
1,615 | <p>You are given the array <code>nums</code> consisting of <code>n</code> positive integers. You computed the sum of all non-empty continuous subarrays from the array and then sorted them in non-decreasing order, creating a new array of <code>n * (n + 1) / 2</code> numbers.</p>
<p><em>Return the sum of the numbers fro... | 0 | {
"code": "class Solution {\npublic:\n int rangeSum(vector<int>& nums, int n, int left, int right) {\n long result =\n (sumOfFirstK(nums, n, right) - sumOfFirstK(nums, n, left - 1)) %\n mod;\n // Ensure non-negative result\n return (result + mod) % mod;\n }\n\nprivate:... |
1,615 | <p>You are given the array <code>nums</code> consisting of <code>n</code> positive integers. You computed the sum of all non-empty continuous subarrays from the array and then sorted them in non-decreasing order, creating a new array of <code>n * (n + 1) / 2</code> numbers.</p>
<p><em>Return the sum of the numbers fro... | 0 | {
"code": "class Solution {\npublic:\n int mod = 1e9 + 7;\n\n pair<int,long long int> findLessThanK(vector<int>& nums,int sum){\n long long int totalSum = 0, singleSum = 0,finalSum = 0;\n int count = 0;\n for(int j=0,i=0;j<nums.size();j++){\n singleSum += nums[j];\n to... |
1,615 | <p>You are given the array <code>nums</code> consisting of <code>n</code> positive integers. You computed the sum of all non-empty continuous subarrays from the array and then sorted them in non-decreasing order, creating a new array of <code>n * (n + 1) / 2</code> numbers.</p>
<p><em>Return the sum of the numbers fro... | 0 | {
"code": "class Solution {\npublic:\n typedef long long ll;\n typedef pair<ll, ll> pll;\n ll n, M = 1e9 + 7;\n vector<int> a;\n pll countNSum(ll sm)\n {\n ll ct = 0, ans = 0;\n for(ll i = 0, j = 0, cur = 0, mx = 0; j < n; j++)\n {\n cur += a[j] * (j - i + 1),\n ... |
1,615 | <p>You are given the array <code>nums</code> consisting of <code>n</code> positive integers. You computed the sum of all non-empty continuous subarrays from the array and then sorted them in non-decreasing order, creating a new array of <code>n * (n + 1) / 2</code> numbers.</p>
<p><em>Return the sum of the numbers fro... | 0 | {
"code": "class Solution {\npublic:\n const int MOD = 1e9 + 7;\n int rangeSum(vector<int>& nums, int n, int left, int right) {\n int ans = 0;\n vector<int> prefixSum = vector<int>(n+1);\n vector<int> prefixPrefixSum = vector<int>(n+1);\n for(int i = 0; i < n; i++) {\n pre... |
1,615 | <p>You are given the array <code>nums</code> consisting of <code>n</code> positive integers. You computed the sum of all non-empty continuous subarrays from the array and then sorted them in non-decreasing order, creating a new array of <code>n * (n + 1) / 2</code> numbers.</p>
<p><em>Return the sum of the numbers fro... | 0 | {
"code": "static int speedup = []()\n{\n ios::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n return 0;\n}();\n\nclass Solution {\npublic:\n int rangeSum(vector<int>& nums, int n, int left, int right) {\n \n int cnt[100001]{}, ans = 0, skip = 0, sum, inclu, take;\n co... |
1,615 | <p>You are given the array <code>nums</code> consisting of <code>n</code> positive integers. You computed the sum of all non-empty continuous subarrays from the array and then sorted them in non-decreasing order, creating a new array of <code>n * (n + 1) / 2</code> numbers.</p>
<p><em>Return the sum of the numbers fro... | 0 | {
"code": "class Solution {\npublic:\n static constexpr int m = 10e8 + 7;\n\n uint32_t rangeSum(const vector<int> &nums, size_t size, uint32_t left, uint32_t right) {\n\n left--;\n right--;\n\n uint32_t leadSum[size + 1];\n leadSum[0] = 0;\n\n for (size_t i = 0; i < size; i++)... |
1,615 | <p>You are given the array <code>nums</code> consisting of <code>n</code> positive integers. You computed the sum of all non-empty continuous subarrays from the array and then sorted them in non-decreasing order, creating a new array of <code>n * (n + 1) / 2</code> numbers.</p>
<p><em>Return the sum of the numbers fro... | 0 | {
"code": "class Solution {\npublic:\n const int M = 1000000007;\n int rangeSum(vector<int>& nums, int n, int left, int right) {\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;\n for(int i=0;i<n;i++){\n pq.push({nums[i],i});\n }\n int ans=0... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.