Home > OS >  Ruby - Hacker Rank issue
Ruby - Hacker Rank issue

Time:09-22

**

Ok guys , i have a problem with a task in Hacker Rank. I can't pass all tests.


**

The task:

Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.

Function Description

Complete the miniMaxSum function in the editor below.

miniMaxSum has the following parameter(s):

arr: an array of 5 integers

Print

Print two space-separated integers on one line: the minimum sum and the maximum sum of 4 of 5 elements.

Input Format

A single line of five space-separated integers.

**That is the task and my code: **


require 'json'
require 'stringio'

#
# Complete the 'miniMaxSum' function below.
#
# The function accepts INTEGER_ARRAY arr as parameter.
#

def miniMaxSum(arr)
   if arr[0] != arr[1]
     $prev = 0 
     $prev2 = Float::INFINITY
     i = 0
     5.times do
       $res = arr.reject { |n| n == arr[i] }.sum
       $prev = $res if $res > $prev
       i = i   1
      end
      i = 0
     5.times do
       $res2 = arr.reject { |n| n == arr[i] }.sum
       $prev2 = $res2 if $res < $prev2
       i = i   1
      end
     print "#{$prev2} #{$prev}"
    else
        print "#{arr.sum} #{arr.sum}"
    end
      
end

arr = gets.rstrip.split.map(&:to_i)

miniMaxSum arr

PLEASE HELP.

CodePudding user response:

Wouldn't it be enough to sum the 4 smallest and the 4 biggest elements in the array?

Your question is a bit unclear about in input, therefore I provide two answers.

When the input is an array of 5 integers:

def min_max_sum(array)
  sorted = array.sort
  puts "#{sorted.first(4).sum}  #{sorted.last(4).sum}"
end

min_max_sum [5, 4, 3, 2, 1]
#=> 10  14

When the input format is a single line of five space-separated integers:

def min_max_sum(string)
  sorted = string.split(/ /).map(&:to_i).sort
  puts "#{sorted.first(4).sum}  #{sorted.last(4).sum}"
end

min_max_sum "5 4 3 2 1"
#=> 10  14

CodePudding user response:

We can observe that the sum of 4 of the 5 elements is simply the sum of all 5 minus one of the elements. If we have [1,2,3,4,5] the sum is 15 and we can get the partial sums by subtracting each from that. 15 - 1 is 14, 15 - 2 is 13, and so on.

Then we can find the min and the max in a single loop.

This is algorithmically more efficient than sorting and summing the largest and the smallest numbers. This algorithm only has to iterate through the list twice. It runs in 2n time, or O(n). Sorting requires O(nlogn) plus two iterations to sum. This doesn't matter for 5 numbers, but it's very important as the size gets larger.

See Time Complexity.

def min_max_sum(nums)
  # Take the sum of every number.
  sum = nums.sum

  # Ye olde min/max trick.
  # Set min to something everything will be smaller than.
  # Set max to something everything will be larger than.
  min = Float::INFINITY
  max = -Float::INFINITY
  
  # For each number...
  nums.each do |num|
    # Get the sum for 4 of 5 values by subtracting this number from the sum.
    partial_sum = sum - num
    
    # Check if it's the min and/or the max.
    min = partial_sum if partial_sum < min
    max = partial_sum if partial_sum > max
  end
  
  return [min, max]
end

CodePudding user response:

Anyway I solved the problem .... I had put $res instead of $res2 in the second loop. Then in the else condition I added arr.pop. But thanks for the answers

#!/bin/ruby

require 'json'
require 'stringio'

#
# Complete the 'miniMaxSum' function below.
#
# The function accepts INTEGER_ARRAY arr as parameter.
#

def miniMaxSum(arr)
   if arr[0] != arr[1]
     $prev = 0 
     $prev2 = Float::INFINITY
     i = 0
     5.times do
       $res = arr.reject { |n| n == arr[i] }.sum
       $prev = $res if $res > $prev
       i = i   1
      end
      i = 0
     5.times do
       $res2 = arr.reject { |n| n == arr[i] }.sum
       $prev2 = $res2 if $res2 < $prev2
       i = i   1
      end
     print "#{$prev2} #{$prev}"
    else
        arr.pop
        res = arr.sum
        print "#{res} #{res}"
    end
      
end

arr = gets.rstrip.split.map(&:to_i)

miniMaxSum arr
  • Related