Please click on 1-2 Ads to keep this website's Study Material FREE and regularly Updated.

Program 14: Write a C++ program to find the maximum element from all the sub-arrays whose length is given by user.

#include<iostream>
using namespace std;

int main()
{
	int size,len;
	cout<<"\nenter the size of an array : ";
	cin>>size;
	int arr[size];
	cout<<"\nenter array elements : ";

	for(int i=0;i<size;i++)
	{
		cin>>arr[i];
	}
	cout<<"\nenter the length of sub array : ";
	cin>>len;
	cout<<"\nsub-arrays and their max element : ";

	for(int i=0;i<=size-len;i++)
	{
		cout<<"\n";
		int max=arr[i];
		for(int j=i;j<i+len;j++)
		{
			cout<<arr[j]<<"\t";
			if(arr[j]>max)
			max=arr[j];
		}
		cout<<"  :  "<<max;
	}
	return 0;	
}



If you have any suggestion or found any error in the above program, then do let us know at [email protected] or just comment below.