Maximum Value of Modulus Expression Problem

You are provided with two arrays ARR1 and ARR2 both having an equal length N. Your objective is to compute the maximum value of the expression:

|ARR1[ i ] - ARR1[ j ]| + |ARR2[ i ] - ARR2[ j ]| + |i - j|, where 0 <= i, j < n and |A| denotes the absolute (non-negative) value of A.

Input:

The first line provides an integer T representing the number of test cases. 
Each test case contains:
- A single integer N, denoting the length of arrays ARR1 and ARR2.
- A line of N space-separated integers describing the elements of array ARR1.
- A line of N space-separated integers describing the elements of array ARR2.

Output:

For each test case, output a single integer indicating the maximum value of the expression. Each test case output should be printed on a separate line.

Example:

Input: 
n = 4, ARR1 = [1, 2, 3, 4], ARR2 = [-1, 3, 4, 2]

Output:
9

Explanation: The maximum value of the expression is found from indices i = 0 and j = 3. Computing the expression:
|ARR1[0] - ARR2[3]| + |ARR2[0] - ARR2[3]| + |0 - 3| = |1 - 4| + |-1 - 2| + |-3| = 3 + 3 + 3 = 9
So the result is 9.

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 1000
  • -10^6 <= ARR1[i], ARR2[i] <= 10^6

Note:

No need to handle input/output operations. Focus on implementing the function to compute the maximum value.
AnswerBot
4mo

Compute the maximum value of a given expression involving two arrays.

  • Iterate through all possible pairs of indices i and j to calculate the expression value

  • Keep track of the maximum value found so far...read more

Help your peers!
Select
Add answer anonymously...

Popular interview questions of Software Developer

A Software Developer was asked Q1. Reverse Linked List Problem Statement Given a Singly Linked List of integers, yo...read more
A Software Developer was asked Q2. Longest Palindromic Substring Problem Statement You are provided with a string S...read more
A Software Developer was asked Q3. 0-1 Knapsack Problem Statement A thief plans to rob a store and can carry a maxi...read more
Algo8 AI 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