Longest Switching Subarray Problem Statement

You are provided with an array 'ARR' consisting of 'N' positive integers. Your task is to determine the length of the longest contiguous subarray that is switching.

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
5d

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!
Add answer anonymously...
TCS Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter