NITS Local #30
Author: amit_dwivedi
There are many ways to solve this problem. One of which is described here.
All we have to do is find the maximum number of indices such that both strings have different values. We can do this by sorting one string in increasing order and another one in decreasing order. Now count of these indices gives us the number of set bits and the remaining are unset bits.
Time Complexity: $$$O(n \log n)$$$
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
string s, t;
cin >> s >> t;
sort(s.begin(), s.end());
sort(t.begin(), t.end());
reverse(t.begin(), t.end());
string ans = "";
for(int i = 0; i < (int) s.size(); i ++) {
if(s[i] != t[i]) ans += "1";
else ans += "0";
}
sort(ans.begin(), ans.end());
reverse(ans.begin(), ans.end());
cout << ans;
return 0;
}
Author: amit_dwivedi
Go for the tutorial of GS more like BS(Hard version)
Author: amit_dwivedi
Like in Kadane's Algorithm, we set our max_ending_here variable to zero if and only if we are on good index and max_ending_here is less than zero.
Time Complexity: $$$O(n)$$$
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n; cin >> n;
vector<int> arr(n), g(n);
for(int i = 0; i < n; i ++) cin >> arr[i];
for(int i = 0; i < n; i ++) cin >> g[i];
ll max_sum_so_far = 0, max_sum = -2e18, st = -1;
for(int i = 0; i < n; i ++) {
if(g[i] == 1) st = 1;
if(g[i] == 1 and max_sum_so_far < 0)
max_sum_so_far = 0;
if(st == -1) continue;
max_sum_so_far += arr[i];
max_sum = max(max_sum, max_sum_so_far);
}
cout << max_sum << endl;
return 0;
}
Author: amit_dwivedi
Let us assume that the first person is the one with lowest skills. Now we iterate though all other applicants to check whether someone has skills less than the assumed one, if yes we change our person to this one, and do the same for remaining. Now, at last, we have a profile with lowest skills. Let the index of this profile is $$$x$$$. Now we check if there is someone who has less skills than the person with profile index $$$x$$$. If it is exist then the answer is straightforward $$$-1$$$, else $$$x$$$ is the answer.
Time Complexity: $$$O(n)$$$
#include "bits/stdc++.h"
using namespace std;
struct student {
int a, b, c;
};
bool operator<(const student& t, const student& o)
{
int cntLess = 0;
if (t.a < o.a)
cntLess++;
if (t.b < o.b)
cntLess++;
if (t.c < o.c)
cntLess++;
return cntLess >= 2;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<student> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i].a >> v[i].b >> v[i].c;
}
int id = 0;
for (int i = 1; i < n; ++i) {
if (v[i] < v[id]) {
id = i;
}
}
for (int i = 0; i < n; i++) {
if (i == id)
continue;
if (v[i] < v[id]) {
cout << -1 << '\n';
return 0;
}
}
cout << id + 1 << '\n';
}
Author: amit_dwivedi
Time Complexity: $$$O()$$$
~~~~~
~~~~~
Author: sprkgoyal
Time Complexity: $$$O(n)$$$
~~~~~
~~~~~
Author: amit_dwivedi
Time Complexity: $$$O()$$$
~~~~~
~~~~~