/*
if array size n = 8
value is : 8
and array is : 5 1 2 3 6 5 3 1
Then Minimum Length Array is : [3 6]
and Minimum Length is : 2
*/
#include<bits/stdc++.h>
using namespace std;
int minimum_length(int a[], int n, int value)
{
int minlength = n+1;
for(int i=0; i<n; i++)
{
int currentsum = a[i];
if(currentsum > value)
return 1;
for(int j=i+1; j<n; j++)
{
currentsum += a[j];
if(currentsum > value && (j-i+1) < minlength)
{
minlength = j-i+1;
break;
}
if(currentsum > value && (j-i+1) > minlength)
break;
}
}
return minlength;
}
int main()
{
int n, value;
cout << "Enter Array Size :: ";
cin >> n;
cout << "\nEnter a Value :: ";
cin >> value;
int a[n]; /// Declar a array
cout << "\nEnter Array Element :: ";
for(int i=0; i<n; i++)
cin >> a[i];
cout << "\n";
int mn = minimum_length(a, n, value);
if(mn != n+1)
cout << "Minimum Length :: " << mn << "\n";
else
cout << "Minimum Length Not Found." << "\n";
return 0;
}
Comments
Post a Comment