# GOOGLE KICK START ROUND-A -Workout(SOLVED)

Can somebody help me find out a test case for which my code fails @ssjgz ?
PROBLEM: Kick Start - Google’s Coding Competitions
MY SOLUTION :

``````#include<bits/stdc++.h>
using namespace std;
typedef long long ll ;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<ll> vi;
typedef vector<pll> vpll;
typedef unordered_map <ll,ll> umap ;
//#pragma GCC optimize "trapv"
#define loop(i,a,b) for(ll i=a ;i<b;i++)
#define For(i,n) for(int i=0;i<(ll)n;i++)
#define Rev(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,n) for(int i=1;i<=n;++i)
#define F first
#define S second
#define pb push_back
#define em emplace_back
#define all(v) (v).begin(),(v).end()
#define mems(x, y) memset(x, y, sizeof(x))
#define sz(v) (v).size()
#define mp(a,b) make_pair(a,b)
#define pf(n) cout<<n<<"\n"
#define pff(n) cout << n << " " ;
long const M=1e9+7;
const long mxN =1e5+2 ;
const long mxNN =1e6+2 ;
ll a[mxN] ;
void solve(){
ll n , k ; cin >> n >> k ;
multiset<ll> v ;
For(i,n){
cin >>a[i]  ;

}
for(int i =1 ; i <n;i++){
v.insert(a[i]-a[i-1]) ;
}
auto it = v.end() ;
it-- ;
while(*it >=1  && k){
ll val = *it ;
v.erase(it) ;
v.insert(val-val/2) ;
v.insert(val/2) ;
it = v.end() ;
it-- ;
k-- ;
}
it = v.end() ;
it-- ;
cout << *it << endl ;

}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//solve() ;
ll t ; cin >> t ;
For(i,t){
cout <<"Case #"<< i+1 << ": ";
solve() ;
}
return 0;
}

``````
2 Likes

check out the analysis its already mention there that dividing by 2 is not optimal

For this test case, we cannot perform such direct splits because repeatedly splitting the maximum difference into halves is not optimal. For example, given a sequence [2, 12] and K = 2, splitting into halves will result in [2, 12] → [2, 7, 12] → [2, 7, 9, 12]. This way, the difficulty would be 5. However, if we perform [2, 12] → [2, 5, 12] → [2, 5, 8, 12], the difficulty would be 4.

4 Likes

Got it thanks

your approach is lokoks right but it will give you a time limit
Because i used same approach and get 11 point but for k>1 && k<=10^5 TLE

Dont you think that he has done that only because he is maintaining difference so there will be 5 only not 7

``````#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<vector>
#include<set>
#include<stack>
#include<queue>
#define ll long long int

using namespace std;

main()
{
ll t;
cin>>t;
ll x=1;
while(t--)
{
ll n,k;
cin>>n>>k;
ll a[n];
vector<ll> v;
for(ll i=0;i<n;i++)
{  cin>>a[i];
v.push_back(a[i]);
}
for(ll i=n-1;i>=0;i--)
v[i]=v[i]-v[i-1];

v[0]=0;

sort(v.begin(),v.end());
while(k--)
{
ll x=v[v.size()-1];
v.pop_back();
ll x1=x/2;
ll x2=x-x1;
v.push_back(x1);
v.push_back(x2);
sort(v.begin(),v.end());
}
cout<<"Case #"<<x<<": "<<v[v.size()-1]<<endl;
x++;
}

}`````````

this wont give optimal ans
look at the example mentioned above

u can decrease time complexity by using multiset instead of sorting array everytime

But still i think it will give TLE . I saw many of doing binary search but i did not understand that approach so if you then please help

My code is giving optimal because already i get 11 point means for small input it is working . BUt for larger iit gives TLE

first set of test cases is only for k=1 which means u can only input one value which works for your algo but it didnt work when k>1 look at this example:

For this test case, we cannot perform such direct splits because repeatedly splitting the maximum difference into halves is not optimal. For example, given a sequence [2, 12] and K = 2, splitting into halves will result in [2, 12] → [2, 7, 12] → [2, 7, 9, 12]. This way, the difficulty would be 5. However, if we perform [2, 12] → [2, 5, 12] → [2, 5, 8, 12], the difficulty would be 4.

1 Like

Thanks.
What optimal algorithm have you used for the k>1 cases? Can you please share.

Bdw , even if you decreased the time complexity , it will give wrong answer. This approach is wrong.

What if I gave you the array and number D and asked to check if you can place K workouts anywhere you want and bring the max difference down to D or less ?

The same approach is being applied, we are searching for the lowest possible D by using binary search.

Suppose Mid= (L+R)/2 ,
now if we can get a max difference time of Mid or less ,then we search in the left half (R=Mid-1) looking for a lower value of D ,

If we can’t get a max difference time of Mid or Less, then obviously the answer is larger than Mid, so we search in the right half (L=MId+1) .