Algorithm
Problem Name: 30 days of code -
In this HackerRank in 30 Days of Code -
problem solution,Objective
Today we’re discussing scope.
The absolute difference between two integers, a and b, is written as |a – b|. The maximum absolute difference between two integers in a set of positive integers, elements, is the largest absolute difference between any two integers in elements.
The Difference class is started for you in the editor. It has a private integer array (elements) for storing N non-negative integers, and a public integer (maximumDifference) for storing the maximum absolute difference.
Task
Complete the Difference class by writing the following:
- A class constructor that takes an array of integers as a parameter and saves it to the _elements instance variable.
- A computeDifference method that finds the maximum absolute difference between any 2 numbers in _elements and stores it in the maximumDifference instance variable.
Input Format
You are not responsible for reading any input from stdin. The locked Solution class in the editor reads in 2 lines of input. The first line contains N, the size of the elements array. The second line has N space-separated integers that describe the _elements array.
Constraints
- 1 <= N <= 10
- 1 <= _elements[i] <= 100, where 0 <= i <= N – 1
Output Format
You are not responsible for printing any output; the Solution class will print the value of the maximumDifference instance variable.
Sample Input
STDIN Function
----- --------
3 __elements[] size N = 3
1 2 5 __elements = [1, 2, 5]
Sample Output
4
Explanation
The scope of the _elements array and maximumDifference integer is the entire class instance. The class constructor saves the argument passed to the constructor as the _elements instance variable (where the computeDifference method can access it).
To find the maximum difference, computeDifference checks each element in the array and finds the maximum difference between any 2 elements: |1 – 2| = 1
|1 – 5| = 4
|2 – 5| = 3
The maximum of these differences is 4, so it saves the value 4 as the maximumDifference instance variable. The locked stub code in the editor then prints the value stored as maximumDifference, which is 4.
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
class Difference {
private:
vector<int> elements;
public:
int maximumDifference;
Difference(vector<int> elements) {
this->elements = elements;
}
void computeDifference() {
int maximum = 0;
for (int i = 0; i < this->elements.size(); i++) {
for (int j = 0; j < this->elements.size(); j++) {
int absolute = abs(elements[i] - elements[j]);
if (absolute > maximum) maximum = absolute;
}
}
maximumDifference = maximum;
}
};
int main() {
int N;
cin >> N;
vector<int> a;
for (int i = 0; i < N; i++) {
int e;
cin >> e;
a.push_back(e);
}
Difference d(a);
d.computeDifference();
cout << d.maximumDifference;
return 0;
}
Copy The Code &
Try With Live Editor
#2 Code Example with C# Programming
Code -
C# Programming
using System;
using System.Linq;
class Difference
{
int[] elements;
public int maximumDifference;
public Difference(int[] elements)
{
this.elements = elements;
}
public void computeDifference()
{
int max = 0;
for (int i = 0; i < elements.Length; i++)
{
for (int j = 0; j < elements.Length; j++)
{
int abs = Math.Abs(elements[i] - elements[j]);
if (abs > max) max = abs;
}
}
maximumDifference = max;
}
}
class Solution
{
static void Main(string[] args)
{
int.Parse(Console.ReadLine());
int[] a = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
Difference d = new Difference(a);
d.computeDifference();
Console.Write(d.maximumDifference);
}
}
Copy The Code &
Try With Live Editor
#3 Code Example with Java Programming
Code -
Java Programming
import java.util.Scanner;
class Difference {
private int[] elements;
public int maximumDifference;
Difference(int[] elements) {
this.elements = elements;
}
public void computeDifference() {
int max = 0;
for (int i = 0; i < elements.length; i++) {
for (int j = 0; j < elements.length; j++) {
int abs = Math.abs(elements[i] - elements[j]);
if (abs > max) max = abs;
}
}
maximumDifference = max;
}
}
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
sc.close();
Difference difference = new Difference(a);
difference.computeDifference();
System.out.print(difference.maximumDifference);
}
}
Copy The Code &
Try With Live Editor
#4 Code Example with Python Programming
Code -
Python Programming
class Difference:
def __init__(self, a):
self.__elements = a
def computeDifference(self):
maximum = 0
for i in range(len(self.__elements)):
for j in range(len(self.__elements)):
absolute = abs(self.__elements[i] - self.__elements[j])
if absolute > maximum:
maximum = absolute
self.maximumDifference = maximum
_ = input()
a = [int(e) for e in input().split(' ')]
d = Difference(a)
d.computeDifference()
print(d.maximumDifference)
Copy The Code &
Try With Live Editor
#5 Code Example with PHP Programming
Code -
PHP Programming
elements = $a;
}
public function ComputeDifference() {
$this->maximumDifference = abs( min( $this->elements ) - max( $this->elements ) );
}
}
$N = intval( fgets( STDIN ) );
$a = array_map( 'intval', explode(' ', fgets( STDIN ) ) );
$d = new Difference( $a );
$d->ComputeDifference();
print( $d->maximumDifference );
?>
Copy The Code &
Try With Live Editor