Why Choose Us?
0% AI Guarantee
Human-written only.
24/7 Support
Anytime, anywhere.
Plagiarism Free
100% Original.
Expert Tutors
Masters & PhDs.
100% Confidential
Your privacy matters.
On-Time Delivery
Never miss a deadline.
Given an array of N integers, you can do some operations on it
Given an array of N integers, you can do some operations on it. In each operation, you may choose two adjacent elements a[i] and a[i+1], and do either of the following two –
1. Replace a[i] with a[i] + a[i+1] and delete a[i+1].
2. Replace a[i+1] with a[i] + a[i+1] and delete a[i].
Note, performing any of these operations will reduce the array size by 1. Find the max array size, you can obtain, by doing these operations, such that, at last, all the elements of the final array are equal.
Print that maximum final size of the array.
Input Format –
The first line contains a single integer N, denoting the number of values in the array. Then, N lines follow, where each line contains a single integer, denoting the array values.
Output Format –
Print a single line, containing a single integer, the answer to the problem.
note
Constraints
1 <= N <= 2000
1 <= array values <= 1,00,000
view_list
Examples
Input:
4
1
2
2
1
Output:
2
Explanation:
We can finally obtain an array, of size 2, where each of the values of the array would be 3.
Expert Solution
int maxSize(vector v)
{
int n = v.size();
vector p(n);
p[0]=v[0];
for(int i=1;i {
p[i]+=p[i-1]+v[i];
}
int x=-1;
for(auto &it:p)
{
int sum=0;
int i=0;
int gc=0;
while(i {
sum+=v[i];
if(sum==it)
{
gc++;
sum=0;
}
else if(sum>it)
{
gc=-1;
break;
}
i++;
}
x=max(x,gc);
}
return n-x;
}
Archived Solution
You have full access to this solution. To save a copy with all formatting and attachments, use the button below.
For ready-to-submit work, please order a fresh solution below.





