Thanks a lot
I tried the solution as per the above explanation but still my code is getting wrong answers.Can anyone tell me where am i wrong??
`#include <bits/stdc++.h>
using namespace std;
void task(vectorvect,int n)
{
int maxm=*max_element(vect.begin(),vect.end());
int ar[n];
for(int i=0;i<n;i++)ar[i]=0;
int k=0,flag=0,count=0;
for(auto it=vect.begin();it!=vect.end();++it)if(*it==maxm)count++;
vector::iterator temp;
vect.insert(vect.end(),vect.begin(),vect.end());
for(auto it=vect.begin();it!=vect.end();++it)
{
if(*it==maxm&&(k+1)<=count)
{
if(flag==0){temp=it;flag++;continue;}
ar[k]=it-temp-1;
printf("%d “,ar[k]);
k++;
temp=it;
}
}int sum=0;
for(int i=0;i<n;i++)
{
ar[i]=max(ar[i]-n/2+1,0);
sum=sum+ar[i];
}
printf(”%d",sum);
}
int main()
{
int t;
int n;
scanf("%d",&t);;
for(int i=0;i<t;i++)
{
scanf("%d",&n);
int ar[n];
for(int j=0;j<n;j++)
scanf("%d",&ar[j]);
vector<int>vect(ar,ar+n);
task(vect,n);
printf("\n");;
}
return 0;
}`
Can anyone please tell me why I am getting a wrong ans for this question? Here is my code–
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t,n,i,temp;
vector w;
vector max_index;
cin>>t;
while(t–!=0)
{
w.clear();
max_index.clear();
cin>>n;
int max_val=INT_MIN;
int k=0;
for(i=0;i<n;i++)
{
cin>>temp;
w.push_back(temp);
if(max_val<temp)
{
max_val=temp;
}
}
for(i=0;i<n;i++)
if(w[i]==max_val)
max_index.push_back(i+1);
if(max_index[0]>n/2) k++;
if(max_index.size()==1){
k+=n-max_index[0];
cout<<k<<endl;
continue;
}
for(i=1;i<max_index.size();i++)
{
if(max_index[i]-max_index[i-1]-1>=n/2)
k+=max_index[i]-max_index[i-1]-n/2;
}
if(n+max_index[0]-max_index[max_index.size()-1]-1>=n/2)
k+=n-max_index[max_index.size()-1];
cout<<k<<endl;
}
}
Thanks!
What is wrong in this please can anyone tell me
#include<bits/stdc++.h>
using namespace std;
int idx1,idx2,moves;
int c=0;
int main()
{
int t;
cin>>t;
if(t<1 || t>5)
exit(0);
while(t!=0)
{
int n;
cin>>n;
if(n<1 || n>100000 || n%2 != 0)
exit(0);
int size=n/2;
int ft[size];
int lt[size];
for(int i=1;i<=size;i++)
{
cin>>ft[i];
if(ft[i] < 1 || ft[i]>100000)
exit(0);
}
for(int j=1;j<=size;j++)
{ cin>>lt[j];
if(lt[j] < 1 || lt[j]>100000)
exit(0);
}
int max1=ft[1];
idx1=1;
for(int i=2;i<=size;i++)
{
if(ft[i]> max1)
{
max1=ft[i];
idx1=i;
}
}
int idx2;
int max2=lt[1];
idx2=1;
for(int i=2;i<=size;i++)
{
if(lt[i]> max2)
{
max2=lt[i];
idx2=i;
}
}
int count =0;
if(max1 >= max2)
{
moves=size+1-idx1;
idx2=size+1-moves;
int i=idx2;
while(i>=1)
{
for(int j=i;j<=size;j++)
{
c=0;
if(lt[j]<max1)
continue;
else
{
c=1;
break;
}
}
if(c==0)
{
count++;
}
i--;
}
}
else
{
cout<<"0"<<endl;
t--;
continue;
}
t--;
cout<<count<<endl;
}
return 0;
}
for(; j<n&&w[j]^mx; ++j);
from above line of code w[j]^mx
I know it is xor operation.But what does this mean ?? why is this code used in editorial solution ? can anyone explain this ??
When is xor 0?
When both the elements are same. Basically iterate until w[j]==mx.
What if N is odd number ? It is not mentioned in question.
My friend c++ is best for CP , you must go for STL in c++
Yes, I will. 
What if N is odd number ? It is not mentioned in question.
Author can u help me out with Your solution I still can’t get ur logic how this work
Nice solution man
this is my code pls tell any 1 t.c on which it will fail
it is giving w.a. on submission
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define vi vector
#define mii map<int,int>
#define pqb priority_queue
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define ps(x,y) fixed<<setprecision(y)<<x
#define w(x) int x;cin>>x;while(x–)
int mod = 1000000007;
class Triplet {
public:
int x;
int y;
int gcd;
};
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
Triplet extendedEuclid(int a, int b) {
if (b == 0) {
Triplet ans;
ans.gcd = a;
ans.x = 1;
ans.y = 0;
return ans;
}
Triplet smallAns = extendedEuclid(b, a % b);
Triplet ans;
ans.gcd = smallAns.gcd;
ans.x = smallAns.y;
ans.y = smallAns.x - (a / b) * smallAns.y;
return ans;
}
int modulo(int a, int m) {
Triplet ans = extendedEuclid(a, m);
return ans.x;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
w(t) {
int n;
cin >> n;
int arr[2 * n];
int maxi = 0;
for (int i = 0; i < n; i++) {
cin >> arr[i];
maxi = max(arr[i], maxi);
}
int s = 2 * n;
for (int i = n; i < s; i++) {
arr[i] = arr[i - n];
}
int i = n + 1 , j = n + n / 2 ;
int ma[s + 1] = {};
ma[0] = 0;
if (arr[0] == maxi)
ma[1] = 1;
for (int i = 1; i <= s; i++) {
if (arr[i] == maxi)
ma[i + 1] = 1;
ma[i + 1] += ma[i];
// cout << ma[i] << ' ';
}
// cout << endl;
int count = 0;
// bool found = false;
while (i > 0) {
// cout << i << ' ' << j << " ";
// cout << ma[j] - ma[i] << " \n";
if (ma[j] - ma[i] == 0 && ma[i] == ma[i - 1]) {
// found = true;
count++;
// break;
}
i--;
j--;
// count++;
}
// if (!found) {
// cout << "0\n";
// // cout << "here\n";
// continue;
// }
cout << count << "\n";
}
return 0;
}
u can see my code same logic as yours but a bit change
https://www.codechef.com/viewsolution/34724696
Can anyone tell me what I am doing wrong here??
I can’t understand why this solution is getting WA verdict??
Thanks in advance.
please check my solution i did something like this
I guess, since max sweetness is 1 therefore chef can not eat according to his requirements.
So the correct answer would be zero.
please provide video editorial for this question