There is no need to use set we can check beauty by prefix sum if it is equal to i*(i+1)/2 or sum-arr[i]+arr[i+1] is equal to i*(i+1)/2 . This approach will solve the question in O(n) for question. 3
In the first question if the array is [2 2 2 2 2 3 3 3 3 3 3] -> converting all elements to 2 would cost 6*2=12 and converting all elements to 3 would cost 5*3=15 , but instead we can convert all the elements to one and the cost would be 11 but your code would output 12 but the optimal is 11 please have a look.
I have checked *for every x from 1 to n* , so when x=1, we get freq[x]=0, so (n-0)*1 is going to give me the optimal answer of 11 for the test case you mentioned. You can look at my code, i run a loop *for(int i=1;i
Can someone pls tell why this code fails i have checked its the same logic #include using namespace std; typedef long long ll; const int dx[] = {1, -1, 0, 0}; const int dy[] = {0, 0, 1, -1}; const int mod = 1e9 + 7; int main() { // your code goes here int t; cin >> t; while(t--){ int n; cin >> n; std::vector arr(n); vector mp(n+1); for(int i=0; i> arr[i]; mp[arr[i]]++; } int ans = 1e9; for(int i=1; i
No, becuz we are also taking negative one (-1) in cosideration from 'vec' while finding 'mx' that's why the change you're talking about will also get counted while finding the 'mx' itself!
#include using namespace std; typedef long long ll; const int MOD = 1e9 + 7; typedef vector vi; typedef vector vll; typedef vector vvi; typedef vector vs; typedef vector vvs; typedef pair pii; typedef vector vpii; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; cin >> t; for (int i = 0; i < t; i++) { ll n, k, h; cin >> n >> k >> h; ll ans = 0; for (ll i = 1; i =h) ans += n; else {
i did it with binary search #include using namespace std; #define ll long long int bool canEscape(ll A, ll B, ll H, ll K) { if (A >= H) return true; if (A N >> K >> H; ll count = 0; ll low = 1; ll high = N; ll minA = 2; while (low
Please fill the feedback form: forms.gle/WdVAj6WUKMxkSbXF6
For problem C you can compute initial beauty in O(n) using pref sum
i was able to do 2 of them... thanks for the explanation , it helps us upsolve our contests.
Great explanation thanks alot to the TLE team 🙌.
Great Explanation
Thnx for the explanations bro, they were great!
There is no need to use set we can check beauty by prefix sum if it is equal to i*(i+1)/2 or sum-arr[i]+arr[i+1] is equal to i*(i+1)/2 . This approach will solve the question in O(n) for question. 3
great explanation💯
b and c explanation was so goood!!
kuch bhi , b m formula kse aaya btaya hi nahi !!
Lovely explanation sir🔥🔥🔥
In the first question if the array is [2 2 2 2 2 3 3 3 3 3 3] -> converting all elements to 2 would cost 6*2=12 and converting all elements to 3 would cost 5*3=15 , but instead we can convert all the elements to one and the cost would be 11 but your code would output 12 but the optimal is 11 please have a look.
I have checked *for every x from 1 to n* ,
so when x=1, we get freq[x]=0, so (n-0)*1 is going to give me the optimal answer of 11 for the test case you mentioned.
You can look at my code, i run a loop *for(int i=1;i
Can someone pls tell why this code fails i have checked its the same logic
#include
using namespace std;
typedef long long ll;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
const int mod = 1e9 + 7;
int main() {
// your code goes here
int t;
cin >> t;
while(t--){
int n;
cin >> n;
std::vector arr(n);
vector mp(n+1);
for(int i=0; i> arr[i];
mp[arr[i]]++;
}
int ans = 1e9;
for(int i=1; i
You need to use "long long" instead of int
In C why initial beauty + mx is the ans ? While swapping element in any subarray to get mx wouldn't the initial beauty will also get affected?
No, becuz we are also taking negative one (-1) in cosideration from 'vec' while finding 'mx' that's why the change you're talking about will also get counted while finding the 'mx' itself!
The code for the second problem is not working : third output is coming in negative .... Can you provide the correct code !!
Use "long long"
I have defined my int as long long, that's y its working.
#include
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
typedef vector vi;
typedef vector vll;
typedef vector vvi;
typedef vector vs;
typedef vector vvs;
typedef pair pii;
typedef vector vpii;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
cin >> t;
for (int i = 0; i < t; i++)
{
ll n, k, h;
cin >> n >> k >> h;
ll ans = 0;
for (ll i = 1; i =h)
ans += n;
else
{
ll r = (k*a-h)/(k-1);
if(r>0) ans+=r;
}
}
cout
i did it with binary search
#include
using namespace std;
#define ll long long int
bool canEscape(ll A, ll B, ll H, ll K) {
if (A >= H) return true;
if (A N >> K >> H;
ll count = 0;
ll low = 1;
ll high = N;
ll minA = 2;
while (low
July batch kab launch hoga?
bhai sab kuch bdia h bas network sahi karlo voice bhot break ho rhi