Find Maximum Length Sub-array with Specific Adjacent Differences

Given an array of integers ‘A’ of length ‘N’, find the maximum length of a sub-array where the absolute difference between adjacent elements is either 0 or 1.

Example:

Input:
A = [2, 4, 6, 7, 6, 9]
Output:
3
Explanation:

The sub-array [6, 7, 6] has adjacent elements with absolute differences of either 0 or 1, and its length is 3, which is the maximum possible.

Input:

T = number of test cases
For each test case:
N = number of elements in the array
Array A with N space-separated integers

Output:

For each test case, output the maximum length of the sub-array as a single integer in a new line.

Constraints:

  • 1 <= T <= 50
  • 1 <= N <= 104
  • -108 <= A[i] <= 108

Time limit: 1 sec.

Note:

No need to print the output; it is handled automatically. Implement the function to output the results.

AnswerBot
4mo

Find the maximum length of a sub-array with adjacent differences of 0 or 1 in an array of integers.

  • Iterate through the array and keep track of the current sub-array length with adjacent differences of...read more

Omprakash Ray
4mo
int tempSum = 0; int sum = 0; int[] arr = {2, 4, 6, 7, 6, 9, 5, 4, 3, 2}; for(int i = 0; i< arr.length-1; i++) { if(Math.abs(arr[i+1]-arr[i]) == 0 || Math.abs(arr[i+1]-arr[i]) == 1) { tempSum = tempSu...read more
Help your peers!
Select
Add answer anonymously...

Visa Software Developer interview questions & answers

A Software Developer was asked Q. How would you design Google Pay?
A Software Developer was asked Q. HLD of recursive
A Software Developer was asked Q. Why did you only pass 10 out of 14 test cases in the 4th question?

Popular interview questions of Software Developer

A Software Developer was asked Q1. How would you design Google Pay?
A Software Developer was asked Q2. HLD of recursive
A Software Developer was asked Q3. Why did you only pass 10 out of 14 test cases in the 4th question?

Top HR questions asked in Visa Software Developer

A Software Developer was asked Q1. Tell me about your project written in [programming language].
A Software Developer was asked Q2. Could you please explain your resume?
A Software Developer was asked Q3. Why do you want to apply for a VISA?
Visa 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