ch
Feedback
Code with Cisco OA Help : Interview Help

Code with Cisco OA Help : Interview Help

前往频道在 Telegram

Codeforces Codechef Leetcode AtCoder GFG CodeStudio All Contests Solutions available.

显示更多
916
订阅者
无数据24 小时
-47
+5530
帖子存档
E solution Find the smallest element index and if the array is sorted from there then print its index otherwise if not sorted print -1

void solve() { int n; cin >> n; vi arr(n); for (int i = 0; i < n; i++) cin >> arr[i]; map freq; for (int i = 0; i < n; i++) freq[arr[i]]++; int res = 0; for (auto e : freq) res += e.second * (e.second - 1) / 2; res += freq[1] * freq[2]; cout << res << endl; } // D // Here it is, enjoy❤️😁

D is also easy, just count the frequency of each element Then add nC2 for every n belongs to frequency and there is special case that 2^4 = 4^2 so just add the product of freq of 2 and freq of 4 😁❤️

B is brute force Check for every factor of n calculate max min for each one and find the max difference between them

Silly mistakes, lol
Silly mistakes, lol

Use LLONG_MAX if you are using INT_MAX

if (n % 3) cout << "First\n"; else cout << "Second\n"; // A

Get ready for Codeforces Div. 3 🔥

D solution Step 1 Push all elements in a single Vector step 2 Sort it step 3 consider 1-indexing and multiply each element with its index and add them

class Solution { public: long long maxSpending(vector>& values) { vector items; for (const auto& it : values) { for (int x : it) { items.push_back(x); } } sort(items.begin(), items.end()); long long ans = 0; long long d = 1; for (int x : items) { ans += d++ * static_cast(x); } return ans; } }; // D

class Solution { public: long long distributeCandies(int n, int limit) { auto comb2 = [](int n) { return 1LL * n * (n - 1) / 2; }; if (n > 3 * limit) { return 0; } long long ans = comb2(n + 2); if (n > limit) { ans -= 3 * comb2(n - limit + 1); } if (n - 2 >= 2 * limit) { ans += 3 * comb2(n - 2 * limit); } return ans; } }; // A and B both

A and B done ✅ Leetcode

Dm me fast 😍