Longest Switching Subarray Problem Statement

Determine the length of the longest contiguous subarray in a given array of positive integers, where the subarray qualifies as 'switching'. An array is defined as switching if all numbers in even positions are identical and all numbers in odd positions are identical.

Example:

Input:
ARR = [1, 4, 1, 4, 3, 2, 3, 0]
Output:
4
Explanation:

The subarray [1, 4, 1, 4] is switching and has length 4, which is the longest.

Input:

The first line contains an integer T, the number of test cases. Each test case consists of:
- An integer N, the size of the array.
- An array ARR of N positive integers.

Output:

For each test case, output the length of the longest switching subarray.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 10^5
  • 1 <= ARR[i] <= 10^8

Note:

No need for explicit printing. Implement the function to return the result.

AnswerBot
4mo

Find the length of the longest switching subarray in a given array of positive integers.

  • Iterate through the array and keep track of the longest switching subarray found so far.

  • Check if the numbers at ...read more

Help your peers!
Select
Add answer anonymously...

American Express Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Left View of a Binary Tree Problem Statement Given a binary tree, your task is t...read more
A Software Developer Intern was asked Q. Minimum Number of Swaps to Sort an Array Find the minimum number of swaps requir...read more
A Software Developer Intern was asked Q. Maximum Non-Adjacent Subsequence Sum Given an array of integers, determine the m...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Left View of a Binary Tree Problem Statement Given a binary tree, your task is t...read more
A Software Developer Intern was asked Q2. Minimum Number of Swaps to Sort an Array Find the minimum number of swaps requir...read more
A Software Developer Intern was asked Q3. Maximum Non-Adjacent Subsequence Sum Given an array of integers, determine the m...read more
American Express Software Developer Intern 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