Reddit
Question
·
2026 Q1
LC Hard - Sum of Sortable Integers - Q3 of Contest
4 replies
Question Details
[https://leetcode.com/problems/sum-of-sortable-integers/](https://leetcode.com/problems/sum-of-sortable-integers/) Ended up successfully solving the question 15-20 mins after the completion of the con
Full Details
[https://leetcode.com/problems/sum-of-sortable-integers/](https://leetcode.com/problems/sum-of-sortable-integers/) Ended up successfully solving the question 15-20 mins after the completion of the contest😭Most probably will lose my Knight batch too😭😭 Here is the solution (pure mathematics): PS: The 2nd question wasn't easy definitely, it had the use of custom comparator in a set as well as a map. Still trying to fathom how 1) 14-15K solved it and 2) 12K of them did it before me (took me around 45 mins). Does LC not catch GPTed code nowadays? class Solution { public: int sortableIntegers(vector<int>& nums) { int n = nums.size(); int ans = 0; bool temp = true; for (int i = 1; i < n; i++){ if (nums[i] < nums[i - 1]){ temp = false; break; } } if (temp) { ans = 1; // cout<<1<<endl; } if (n == 1) return ans; for (int i = 2; i <= n/2; i++){ if (n % i == 0){ // cout<<i<<" : "<<endl; bool t = true; int x = n / i; int prev_max = INT_MIN; for (int j = 0; j < x; j++){ int fall = 0; int mini = nums[j * i]; int glob_min = nums[j * i]; int glob_max = nums[j * i]; for (int k = j * i + 1; k < (j + 1) * i; k++){ int lol = 1; glob_min = min(glob_min, nums[k]); glob_max = max(glob_max, nums[k]); if (!fall && nums[k] >= nums[k - 1]) continue; if (!fall && nums[k] < nums[k - 1]) { if (nums[k] <= glob_min){ fall = 1; lol = 0; } else { t = false; break; } } if (fall && lol && nums[k] < nums[k - 1]) { t = false; break; } if (fall && lol && nums[k] > mini){ t = false; break; } } if (glob_min < prev_max){ t = false; } // if (t) { // cout<<glob_min<<" "<<prev_max<<endl; // } prev_max = glob_max; if (!t) break; } if (t) { // cout<<i<<endl; ans += i; } } } int fall = 0; int mini = nums[0]; int lol = 0; for (int i = 1; i < n; i++){ lol = 1; if (!fall && nums[i] >= nums[i - 1]) continue; if (!fall && nums[i] < nums[i - 1]) { fall = 1; lol = 0; } if (fall && nums[i] > mini){ return ans; } if (fall && lol && nums[i] < nums[i - 1]) { return ans; } } // cout<<n<<endl; return ans + n; } };
Free preview — Unlock all questions →