QM6B editorial

PROBLEM LINK:

Practice

Contest

Author: Chandan Boruah

Tester: Chandan Boruah

Editorialist: Chandan Boruah

DIFFICULTY:

EASY

PREREQUISITES:

Implementation

PROBLEM:

Given an array of integers find the minimum of the maximum of all the contiguous subarrays of given length k.

QUICK EXPLANATION:

Use nested iteration to find the maximums of the various subarrays and then print the minimum of them.

EXPLANATION:

Iterate over the array and then find the maximum of the subarrays of length k formed from the current index of the outer loop. Then have another variable to store the value of all those maximums. Please refer code below.

Solution in CS:

using System;
class some
{
	public static void Main()
	{
		int t=int.Parse(Console.ReadLine());
		for(int i=0;i<t;i++)
		{
			string[]kk=Console.ReadLine().Split();
			int n=int.Parse(kk[0]);
			int k=int.Parse(kk[1]);
			int[]arr=new int[n];
			kk=Console.ReadLine().Split();
			for(int j=0;j<n;j++)
			{
				arr[j]=int.Parse(kk[j]);
			}
			int min=int.MaxValue;
			for(int j=0;j<=n-k;j++)
			{
				int max=0;
				for(int l=j,tt=0;tt<k;tt++)
				{
					max=Math.Max(arr[l+tt],max);
				}
				min=Math.Min(max,min);
			}
			Console.WriteLine(min);
		}
	}
}

Note: This problem has bug in constraints (the intended solution doesn’t work for such large constraints), due to my inadequte testing. Will try to host a better contest next time.