Find ratio of zeroes, positive numbers and negative numbers in the Array
Last Updated :
24 Nov, 2022
Given an array a of integers of size N integers, the task is to find the ratio of positive numbers, negative numbers and zeros in the array up to four decimal places.
Examples:
Input: a[] = {2, -1, 5, 6, 0, -3}
Output: 0.5000 0.3333 0.1667
There are 3 positive, 2 negative, and 1 zero. Their ratio would be positive: 3/6 = 0.5000, negative: 2/6 = 0.3333, and zero: 1/6 = 0.1667.
Input: a[] = {4, 0, -2, -9, -7, 1}
Output: 0.3333 0.5000 0.1667
There are 2 positive, 3 negative, and 1 zero. Their ratio would be positive: 2/6 = 0.3333, negative: 3/6 = 0.5000, and zero: 1/6 = 0.1667.
Approach:
- Count the total number of positive elements in the array.
- Count the total number of negative elements in the array.
- Count the total number of zero elements in the array.
- Divide the total number of positive elements, negative elements, and zero elements by the size of the array, to get the ratio.
- Print the ratio of positive, negative, and zero elements in the array up to four decimal places.
Below is the implementation of the above approach.
C++
#include<bits/stdc++.h>
using namespace std;
void positiveNegativeZero( int arr[], int len)
{
float positiveCount = 0;
float negativeCount = 0;
float zeroCount = 0;
for ( int i = 0; i < len; i++) {
if (arr[i] > 0) {
positiveCount++;
}
else if (arr[i] < 0) {
negativeCount++;
}
else if (arr[i] == 0) {
zeroCount++;
}
}
cout << fixed << setprecision(4) << (positiveCount / len)<< " " ;
cout << fixed << setprecision(4) << (negativeCount / len)<< " " ;
cout << fixed << setprecision(4) << (zeroCount / len);
cout << endl;
}
int main()
{
int a1[] = { 2, -1, 5, 6, 0, -3 };
int len= sizeof (a1)/ sizeof (a1[0]);
positiveNegativeZero(a1,len);
int a2[] = { 4, 0, -2, -9, -7, 1 };
len= sizeof (a2)/ sizeof (a2[0]);
positiveNegativeZero(a2,len);
}
|
Java
class GFG {
static void positiveNegativeZero( int [] arr)
{
int len = arr.length;
float positiveCount = 0 ;
float negativeCount = 0 ;
float zeroCount = 0 ;
for ( int i = 0 ; i < len; i++) {
if (arr[i] > 0 ) {
positiveCount++;
}
else if (arr[i] < 0 ) {
negativeCount++;
}
else if (arr[i] == 0 ) {
zeroCount++;
}
}
System.out.printf( "%1.4f " , positiveCount / len);
System.out.printf( "%1.4f " , negativeCount / len);
System.out.printf( "%1.4f " , zeroCount / len);
System.out.println();
}
public static void main(String args[])
{
int [] a1 = { 2 , - 1 , 5 , 6 , 0 , - 3 };
positiveNegativeZero(a1);
int [] a2 = { 4 , 0 , - 2 , - 9 , - 7 , 1 };
positiveNegativeZero(a2);
}
}
|
Python3
def positiveNegativeZero(arr):
length = len (arr);
positiveCount = 0 ;
negativeCount = 0 ;
zeroCount = 0 ;
for i in range (length):
if (arr[i] > 0 ):
positiveCount + = 1 ;
elif (arr[i] < 0 ):
negativeCount + = 1 ;
elif (arr[i] = = 0 ):
zeroCount + = 1 ;
print ( "{0:.4f}" . format ((positiveCount / length)), end = " " );
print ( "%1.4f " % (negativeCount / length), end = " " );
print ( "%1.4f " % (zeroCount / length), end = " " );
print ();
if __name__ = = '__main__' :
a1 = [ 2 , - 1 , 5 , 6 , 0 , - 3 ];
positiveNegativeZero(a1);
a2 = [ 4 , 0 , - 2 , - 9 , - 7 , 1 ];
positiveNegativeZero(a2);
|
C#
using System;
class GFG {
static void positiveNegativeZero( int [] arr)
{
int len = arr.Length;
float positiveCount = 0;
float negativeCount = 0;
float zeroCount = 0;
for ( int i = 0; i < len; i++) {
if (arr[i] > 0) {
positiveCount++;
}
else if (arr[i] < 0) {
negativeCount++;
}
else if (arr[i] == 0) {
zeroCount++;
}
}
Console.Write( "{0:F4} " , positiveCount / len);
Console.Write( "{0:F4} " , negativeCount / len);
Console.Write( "{0:F4} " , zeroCount / len);
Console.WriteLine();
}
public static void Main(String []args)
{
int [] a1 = { 2, -1, 5, 6, 0, -3 };
positiveNegativeZero(a1);
int [] a2 = { 4, 0, -2, -9, -7, 1 };
positiveNegativeZero(a2);
}
}
|
Javascript
<script>
function positiveNegativeZero(arr)
{
let len = arr.length;
let positiveCount = 0;
let negativeCount = 0;
let zeroCount = 0;
for (let i = 0; i < len; i++)
{
if (arr[i] > 0)
{
positiveCount++;
}
else if (arr[i] < 0)
{
negativeCount++;
}
else if (arr[i] == 0)
{
zeroCount++;
}
}
document.write((positiveCount / len).toFixed(4) + " " );
document.write((negativeCount / len).toFixed(4) + " " );
document.write((zeroCount / len).toFixed(4));
document.write( "<br>" );
}
let a1 = [ 2, -1, 5, 6, 0, -3 ];
positiveNegativeZero(a1);
let a2 = [ 4, 0, -2, -9, -7, 1 ];
positiveNegativeZero(a2);
</script>
|
Output:
0.5000 0.3333 0.1667
0.3333 0.5000 0.1667
Time Complexity: O(len)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...