We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Your test to determine if the test case is solvable or not is great, it helped me when i was struggling with the solution, but I came up with a different way to get the required number of loaves needed.
#include<cmath>#include<cstdio>#include<vector>#include<math.h>#include<iostream>#include<algorithm>#define forn(i,n) for(int i = 0;i<n;i++)usingnamespacestd;intmain(){intn,c=0,ans=0,t=0,k;cin>>n;vector<int>a(n);forn(i,n){cin>>a[i];if(a[i]%2!=0){c++;if(t==0){k=i;t=1;}else{ans+=2*(i-k);t=0;}}}if(c%2!=0){cout<<"NO";}else{cout<<ans;}return0;}
Logic I used was that loaves would be given only to the people in between (including them too) 2 people having odd numbers of loaves, I counted the distance between each pairs of such people.
P.S, I am a newbie, Hope I made a valuable contribution and successfully explained my logic
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Fair Rations
You are viewing a single comment's thread. Return to all comments →
Your test to determine if the test case is solvable or not is great, it helped me when i was struggling with the solution, but I came up with a different way to get the required number of loaves needed.
Logic I used was that loaves would be given only to the people in between (including them too) 2 people having odd numbers of loaves, I counted the distance between each pairs of such people.
P.S, I am a newbie, Hope I made a valuable contribution and successfully explained my logic