Infosys On Campus Coding Question In Interview
Best Study Slot – Maximum Problems in K Consecutive Hours
Difficulty: Easy
Topic: Sliding Window
Language: Java
Suitable For: Placement Preparation, Infosys-Style Coding Practice
Problem Statement
A student is preparing for campus placements and studies for N consecutive hours.
For every hour, the student records the number of problems solved during that hour.
The student wants to identify the best continuous study slot of exactly K hours.
Your task is to find the maximum number of problems solved during any K consecutive hours.
The study hours must be consecutive.
Input Format
The first line contains two integers:
N K
N— total number of study hours.K— length of the study slot.
The second line contains N integers:
problems[0] problems[1] ... problems[N-1]
where problems[i] represents the number of problems solved during the i-th hour.
Output Format
Print the maximum number of problems solved in any K consecutive hours.
Example 1
Input
8 3
2 5 1 0 4 3 6 2
Output
13
Explanation
We need to consider every group of exactly 3 consecutive hours.
2 + 5 + 1 = 8
5 + 1 + 0 = 6
1 + 0 + 4 = 5
0 + 4 + 3 = 7
4 + 3 + 6 = 13
3 + 6 + 2 = 11
The maximum is:
4 + 3 + 6 = 13
Therefore:
Answer = 13
Approach 1 – Brute Force
The simplest approach is to calculate the sum of every possible group of K consecutive elements.
For each starting position, calculate:
arr[i] + arr[i+1] + ... + arr[i+k-1]
This works, but the same elements are added repeatedly.
For example:
[2, 5, 1]
has sum 8.
The next window is:
[5, 1, 0]
Instead of calculating the entire sum again, we can:
- Remove
2. - Add
0.
This leads to the Sliding Window technique.
Approach 2 – Sliding Window
First calculate the sum of the first K elements.
For:
2 5 1 0 4 3 6 2
and:
K = 3
the first window is:
2 + 5 + 1 = 8
Now move the window one position to the right.
Remove the element leaving the window:
2
and add the new element:
0
New sum:
8 - 2 + 0 = 6
We continue this process until the end of the array.
This allows us to process every window in constant time.
Java Solution
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
// Sum of the first window
int windowSum = 0;
for (int i = 0; i < k; i++) {
windowSum += arr[i];
}
int maxSum = windowSum;
// Slide the window
for (int i = k; i < n; i++) {
// Add the new element
windowSum += arr[i];
// Remove the element leaving the window
windowSum -= arr[i - k];
maxSum = Math.max(maxSum, windowSum);
}
System.out.println(maxSum);
}
}
Dry Run
Consider:
arr = [2, 5, 1, 0, 4, 3, 6, 2]
K = 3
Initial window:
[2, 5, 1]
Sum:
8
Move right:
[5, 1, 0]
New sum:
8 - 2 + 0 = 6
Next:
[1, 0, 4]
6 - 5 + 4 = 5
Next:
[0, 4, 3]
5 - 1 + 3 = 7
Next:
[4, 3, 6]
7 - 0 + 6 = 13
Next:
[3, 6, 2]
13 - 4 + 2 = 11
Maximum:
13
Complexity Analysis
Brute Force
For every window, we may calculate up to K elements.
Time: O(N × K)
Space: O(1)
Sliding Window
Each element is added to the window once and removed once.
Time: O(N)
Space: O(1)
Therefore, Sliding Window is significantly more efficient.
Important Pattern to Remember
Whenever a problem asks for:
Maximum / minimum / sum / average of exactly
Kconsecutive elements
think about the Sliding Window technique.
Instead of recalculating every window:
new window sum
=
previous window sum
- element leaving
+ element entering
This simple observation reduces the solution from O(N × K) to O(N).
Test Case
Try solving this without looking at the solution:
7 2
4 7 2 8 5 1 6
Expected output:
13
Because:
8 + 5 = 13
Concept: Sliding Window
Difficulty: Easy
Time Complexity: O(N)
Space Complexity: O(1)
A WordPress Commenter
August 17, 2026 at 1:48 amHi, this is a comment.
To get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.
Commenter avatars come from Gravatar.
getyourfirstjob
August 17, 2026 at 7:18 amBKBN