Maximum Difference

Easy
0/40
Average time to solve is 15m
11 upvotes
Asked in companies
PayPalOlaRaja Software Labs (RSL)

Problem statement

You are given an array 'ARR' of the 'N' element. Your task is to find the maximum difference between any of the two elements from 'ARR'.

If the maximum difference is even print “EVEN” without quotes. If the maximum difference is odd print “ODD” without quotes.

For example:

Given 'ARR' = [1, 10, 5, 2, 8, 1 ] , answer is "ODD".
Here the maximum difference is between 10 and 1, 10 - 1 = 9
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of input contains an integer T’ denoting the number of test cases to run. Then the test case follows.

The first line of each test case contains ‘N’, the length of the array 'ARR'.

The second line of each test case contains ‘N’ space-separated elements of 'ARR'.
Output Format:
For each test case, print a single line containing “ODD” without quotes if the maximum difference is odd else print “EVEN” without quotes.

The output for each test case is printed in a separate line.
Note:
You don’t need to print anything. It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 100
1 <= N <= 5000 
1 <= ARR[i] <= 10 ^ 9

where ‘N’ is the length of the array 'ARR', and 'ARR[i]' is an element of the 'ARR' respectively.

Time limit: 1 sec.
Sample Input 1:
2
4
2 9 3 4
6
1 1 1 1 1 1 
Sample Output 1:
ODD
EVEN
Explanation of Sample Input 1:
For the first test case, the maximum difference is 7, between 9 and 2.

For the second test case, all elements are the same, the maximum difference is 0.
Sample Input 2:
2
2
567 11
1
28
Sample Output 2:
EVEN
EVEN 
Hint

The maximum difference is obtained when two elements are the maximum and minimum of the ARR respectively.

Approaches (1)
Find Max And Min

We will iterate over ARR and find the MAX and MIN of the ARR. And if the MAX - MIN is odd the return “ODD” else return "EVEN". 

The algorithm will be-

  1. MAX = -1, MIN= 10 ^ 9 + 1
  2. For VAL in ARR.
    1. If MAX < VAL
      1. MAX = VAL
    2. If MIN > VAL
      1. MIN = VAL
  3. IF (MAX-MIN) % 2
    1. Return ODD
  4. ELSE
    1. Return EVEN
Time Complexity

O(N), where N is the size of ‘ARR’.

 

We will iterate over ARR to find MAX and MIN of the ARR.

Space Complexity

O(1).

 

Since constant space is used.

Code Solution
(100% EXP penalty)
Maximum Difference
Full screen
Console