DEV Community

Akande Joshua
Akande Joshua

Posted on

HackerRank: Plus Minus solution using PHP (Algorithm)

Problem

Given an array of integers, calculate the ratios of its elements that are positive, negative, and zero. Print the decimal value of each fraction on a new line with places after the decimal.

Note: This challenge introduces precision problems. The test cases are scaled to six decimal places, though answers with absolute error of up to are acceptable.

Function Description

Complete the plusMinus function in the editor below.

plusMinus has the following parameter(s):

int arr[n]: an array of integers
Enter fullscreen mode Exit fullscreen mode

Print
Print the ratios of positive, negative and zero values in the array. Each value should be printed on a separate line with digits after the decimal. The function should not return a value.

Input Format
The first line contains an integer, n, the size of the array.
The second line contains n space-separated integers that describe arr[n].

Output Format
Print the following lines, each to decimals:

  • proportion of positive values
  • proportion of negative values
  • proportion of zeros

Solution (How i solved it)

  • Take three variables named "positive", "negative" and "neutral. Store initial value as zero(0).
  • Iterate a for loop through the given array.
  • While iterating the array we have to add to either the positive, negative and neutral values.
  • Calculate the ratios by dividing with the array length.
  • Print the results. Here is a tricky part, The instruction says, print each result on new line, if you are using echo or print function, it wont go to new line, so my solution is to use PHP_EOF.
$postive = 0;
    $negative = 0;
    $neutral = 0;
    $arrsize  = sizeof($arr);
    for ($i = 0; $i < $arrsize; $i++){
        if ($arr[$i] > 0){
            $positive++;
        }else if ($arr[$i] < 0){
            $negative++;
        }else if ($arr[$i] === 0){
            $neutral++;
        }
    }
$pos_res = number_format($positive / $arrsize, $arrsize);
$neg_res = number_format($negative/ $arrsize, $arrsize);
$neu_res = number_format($neutral / $arrsize, $arrsize);

echo $pos_res . PHP_EOL;
echo $neg_res . PHP_EOL;
echo $neu_res. PHP_EOL;
Enter fullscreen mode Exit fullscreen mode

Link to the HackerRank: Plus Minus Test

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay