Sort by

recency

|

1114 Discussions

|

  • + 0 comments

    My JavaScript Solution

    function main() {
        const n = parseInt(readLine().trim(), 10);
        const arr = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));
        let maxDiff = 0;
        let diff = 0;
        for (let i = 0; i < n-1; i++) {
            for (let j = i+1; j < n; j++){
                diff = Math.abs(arr[i] - arr[j]);
                if (diff > maxDiff) maxDiff = diff;
            }
        }
        console.log(maxDiff);
    }
    
  • + 0 comments

    C++ Solution

    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    class Difference {
        private:
        vector<int> elements;
      
      	public:
      	int maximumDifference;
    
    	// Add your code here
        Difference(vector<int> elements) {
            this->elements = elements;
        }
        
        void computeDifference() {
            int max = 0;
            
            for (int i=0; i < elements.size(); i++) {
                for (int j=0; j < elements.size(); j++) {
                    if (i <= j) {
                        continue;
                    }
                    
                    max = std::max(max, abs(elements[i] - elements[j]));
                }
            }
            
            this->maximumDifference = max;
        }
    
    }; // End of Difference class
    
    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;
    }
    
  • + 0 comments

    Difference(vectorelements){ this->elements = elements; }

    void computeDifference(){
        sort(elements.begin(),elements.end());
        this->maximumDifference = 
            abs(elements[elements.size()-1]-elements[0]);
    }
    
  • + 0 comments

    Nobody said I can't sort the elements vector, so I used std::sort

    Difference(vector<int>elements){
            this->elements = elements;
        }
        
        void computeDifference(){
            sort(elements.begin(),elements.end());
            this->maximumDifference = 
                abs(elements[elements.size()-1]-elements[0]);
        }
    
  • + 0 comments
    // Add your code here
    Difference(vector<int> elems) : elements(elems) {}
        
    void computeDifference() {
        vector<int> elementsCopy = elements;
        sort(elementsCopy.begin(), elementsCopy.end());
            
        this->maximumDifference = elementsCopy[elementsCopy.size() - 1] - elementsCopy[0];
    }