Day 14: Scope | 30 Days of Coding | HackerRank | Java Language

**Before watching this make sure that you’ve tried enough**
Language: Java 7

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;


class Difference {
  	private int[] elements;
  	public int maximumDifference;

    // Add your code here

        private int dif;

        Difference(int [] a)
        {
            this.elements = a;
        }

        void computeDifference()
        {
        for(int i = 0;i < elements.length;i++){
            for(int j = 1;j < elements.length;j++){
                dif = Math.abs(elements[i] - elements[j]);
                
                if(dif > maximumDifference){
                    maximumDifference = dif;
                }
            }
        }
    }

    
} // End of Difference class

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);
    }
}

 

If this solution helped you, please share this with your friends.
And if you have anything to say, let me know in the comment section.
More solutions are coming so Like our Facebook page and Subscribe to our YouTube channel to stay connected.

Leave a Comment