Longest Switching Subarray Problem Statement

You are given an array ‘ARR’ containing ‘N’ positive integers. Your objective is to find the length of the longest contiguous subarray that exhibits switching behavior.

A subarray is described as switching if all elements at even indices are equal and all elements at odd indices are equal.

Input:
The input starts with an integer T, indicating the number of test cases.
Each test case is as follows:
The first line contains an integer 'N', the length of the array.
The second line contains N space-separated positive integers representing the array elements.
Output:
For each test case, output a single integer representing the length of the longest switching subarray.
Example:
Input:
1
8
1 4 1 4 3 2 3 0
Output:
4

Explanation: Some examples of switching subarrays are {1, 4, 1, 4}, {3, 2, 3}. The longest among these is {1, 4, 1, 4}, resulting in an output length of 4.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 105
  • 1 <= ARR[i] <= 108
Note:

There is no need for printing; the function should return the expected output for each test case.

AnswerBot
4mo

Find the length of the longest contiguous subarray where elements at even indices are equal and elements at odd indices are equal.

  • Iterate through the array and keep track of the length of the current ...read more

Help your peers!
Select
Add answer anonymously...

TCS Software Developer interview questions & answers

A Software Developer was asked 5d agoQ. What is Python?
A Software Developer was asked 3w agoQ. What is the process for synthesizing data to train a machine learning model with...read more
A Software Developer was asked 1mo agoQ. What is a string?

Popular interview questions of Software Developer

A Software Developer was asked 3w agoQ1. What is the process for synthesizing data to train a machine learning model with...read more
A Software Developer was asked 1mo agoQ2. What is a string?
A Software Developer was asked 2mo agoQ3. What is the concept of Zero Copy Cloning in Snowflake?

Top HR questions asked in TCS Software Developer

A Software Developer was asked 5d agoQ1. Can you provide an explanation of your project?
A Software Developer was asked 2mo agoQ2. Why should we choose you?
A Software Developer was asked 2mo agoQ3. What are your hobbies?
TCS Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits