hi, let Avik connect you.
** You can give your thought about this content and request modifications if needed from Ask For Modifications page. Thank You.
** You can check all the posts on the Posts page.
** More about me in the About Me section.
Maximum Difference
Problem: Maximum Difference
Problem Link: www.codingninjas.com/codestudio/problems/maximum-difference_1262275
Companies Asked: Google, OLA
Approach:
Find the maximum and minimum elements from the given array.
Calculate the difference between the maximum and minimum elements.
Return "ODD" if the difference is ODD, "EVEN" otherwise.
Time and Space Complexity:
Time Complexity: O(N)
Space Complexity: O(1)
Code [C++]
#include <bits/stdc++.h>
string maximumDifference(int n, vector<int>& arr)
{
int maxVal = *max_element(arr.begin(), arr.end());
int minVal = *min_element(arr.begin(), arr.end());
int diff = maxVal - minVal;
return (diff & 1) ? "ODD": "EVEN";
}