ANITGUY2 - Editorial

Problem link: CodeChef: Practical coding for everyone

DIFFICULTY:

Simple

Solution:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {

ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll t;
cin>>t;
while(t--){
    ll n,k;
    cin>>k>>n;
    ll arr[n];
    for(int i=0;i<n;i++)cin>>arr[i];
    ll product=0,p=1,l=0;
    for(int i=0;i<n;i++){
        p*=arr[i];
        while(p>=k && l<=i){
            product+=i-l;
            p/=arr[l++];
        }
    }
    p=(n-l)*(n-l+1)/2;
    cout<<p+product<<"\n";
}
//code
return 0;

}

Run Time: 0.3 sec

Thanks and Regards

Would you please explain this solution, that would be really helpful

Please explain the last step.

Thanks for sheer display of ignorance and irresponsibility.

#include<bits/stdc++.h>
#define fast ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define arin(x,n) for(int i=0;i<n;i++)cin>>x[i]
#define debug(x,n) for(int i=0;i<n;i++)cout<<x[i]<<" "
#define pb push_back
typedef long long int ll;

using namespace std;

void solve()
{
ll n,k;
cin>>k>>n;

ll arr[n];
arin(arr,n);

ll start = 0, end = 0,count = 0, sum = arr[0]; 
while (start < n && end < n) { 
	if (sum < k) { 
		end++; 
		if (end >= start) 
			count += end - start; 
		if (end < n) 
			sum *= arr[end]; 
	}
	else { 
		sum /= arr[start]; 
		start++; 
	} 
} 

cout<<count<<"\n"; 

}

int main()
{
fast;
ll t=1;
cin>>t;
while(t–)
solve();
return 0;
}

why this code giving wrong solution can you explain?