Sort by

recency

|

1112 Discussions

|

  • + 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];
    }
    
  • + 0 comments

    My python solution:

    def computeDifference(self):
            self.maximumDifference = max(self.__elements) - min(self.__elements)
    
  • + 0 comments

    Java Solution :

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