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++
// C++ program to find the ratio of positive, // negative, and zero elements in the array. #include<bits/stdc++.h> using namespace std; // Function to find the ratio of // positive, negative, and zero elements void positiveNegativeZero(int arr[], int len) { // Initialize the postiveCount, negativeCount, and // zeroCountby 0 which will count the total number // of positive, negative and zero elements float positiveCount = 0; float negativeCount = 0; float zeroCount = 0; // Traverse the array and count the total number of // positive, negative, and zero elements. for (int i = 0; i < len; i++) { if (arr[i] > 0) { positiveCount++; } else if (arr[i] < 0) { negativeCount++; } else if (arr[i] == 0) { zeroCount++; } } // Print the ratio of positive, // negative, and zero elements // in the array up to four decimal places. cout << fixed << setprecision(4) << (positiveCount / len)<<" "; cout << fixed << setprecision(4) << (negativeCount / len)<<" "; cout << fixed << setprecision(4) << (zeroCount / len); cout << endl; } // Driver Code. int main() { // Test Case 1: int a1[] = { 2, -1, 5, 6, 0, -3 }; int len=sizeof(a1)/sizeof(a1[0]); positiveNegativeZero(a1,len); // Test Case 2: int a2[] = { 4, 0, -2, -9, -7, 1 }; len=sizeof(a2)/sizeof(a2[0]); positiveNegativeZero(a2,len); } // This code is contributed by chitranayal |
Java
// Java program to find the ratio of positive, // negative, and zero elements in the array. class GFG { // Function to find the ratio of // positive, negative, and zero elements static void positiveNegativeZero(int[] arr) { // Store the array length into the variable len. int len = arr.length; // Initialize the postiveCount, negativeCount, and // zeroCountby 0 which will count the total number // of positive, negative and zero elements float positiveCount = 0; float negativeCount = 0; float zeroCount = 0; // Traverse the array and count the total number of // positive, negative, and zero elements. for (int i = 0; i < len; i++) { if (arr[i] > 0) { positiveCount++; } else if (arr[i] < 0) { negativeCount++; } else if (arr[i] == 0) { zeroCount++; } } // Print the ratio of positive, // negative, and zero elements // in the array up to four decimal places. System.out.printf("%1.4f ", positiveCount / len); System.out.printf("%1.4f ", negativeCount / len); System.out.printf("%1.4f ", zeroCount / len); System.out.println(); } // Driver Code. public static void main(String args[]) { // Test Case 1: int[] a1 = { 2, -1, 5, 6, 0, -3 }; positiveNegativeZero(a1); // Test Case 2: int[] a2 = { 4, 0, -2, -9, -7, 1 }; positiveNegativeZero(a2); } } |
Python3
# Python3 program to find the ratio of positive, # negative, and zero elements in the array. # Function to find the ratio of # positive, negative, and zero elements def positiveNegativeZero(arr): # Store the array length into the variable len. length = len(arr); # Initialize the postiveCount, negativeCount, and # zeroCountby 0 which will count the total number # of positive, negative and zero elements positiveCount = 0; negativeCount = 0; zeroCount = 0; # Traverse the array and count the total number of # positive, negative, and zero elements. for i in range(length): if (arr[i] > 0): positiveCount += 1; elif(arr[i] < 0): negativeCount += 1; elif(arr[i] == 0): zeroCount += 1; # Print the ratio of positive, # negative, and zero elements # in the array up to four decimal places. print("{0:.4f}".format((positiveCount / length)), end=" "); print("%1.4f "%(negativeCount / length), end=" "); print("%1.4f "%(zeroCount / length), end=" "); print(); # Driver Code. if __name__ == '__main__': # Test Case 1: a1 = [ 2, -1, 5, 6, 0, -3 ]; positiveNegativeZero(a1); # Test Case 2: a2 = [ 4, 0, -2, -9, -7, 1 ]; positiveNegativeZero(a2); # This code is contributed by Rajput-Ji |
C#
// C# program to find the ratio of positive, // negative, and zero elements in the array. using System; class GFG { // Function to find the ratio of // positive, negative, and zero elements static void positiveNegativeZero(int[] arr) { // Store the array length into the variable len. int len = arr.Length; // Initialize the postiveCount, negativeCount, and // zeroCountby 0 which will count the total number // of positive, negative and zero elements float positiveCount = 0; float negativeCount = 0; float zeroCount = 0; // Traverse the array and count the total number of // positive, negative, and zero elements. for (int i = 0; i < len; i++) { if (arr[i] > 0) { positiveCount++; } else if (arr[i] < 0) { negativeCount++; } else if (arr[i] == 0) { zeroCount++; } } // Print the ratio of positive, // negative, and zero elements // in the array up to four decimal places. Console.Write("{0:F4} ", positiveCount / len); Console.Write("{0:F4} ", negativeCount / len); Console.Write("{0:F4} ", zeroCount / len); Console.WriteLine(); } // Driver Code. public static void Main(String []args) { // Test Case 1: int[] a1 = { 2, -1, 5, 6, 0, -3 }; positiveNegativeZero(a1); // Test Case 2: int[] a2 = { 4, 0, -2, -9, -7, 1 }; positiveNegativeZero(a2); } } // This code is contributed by sapnasingh4991 |
0.5000 0.3333 0.1667 0.3333 0.5000 0.1667
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- C program to count Positive and Negative numbers in an Array
- Check if array elements are consecutive in O(n) time and O(1) space (Handles Both Positive and negative numbers)
- Rearrange positive and negative numbers in O(n) time and O(1) extra space
- Rearrange positive and negative numbers using inbuilt sort function
- Lambda expression in Python to rearrange positive and negative numbers
- Python program to count positive and negative numbers in a list
- Replace all elements by difference of sums of positive and negative numbers after that element
- Move all negative numbers to beginning and positive to end with constant extra space
- Program to find the common ratio of three numbers
- Only integer with positive value in positive negative value in array
- Find subarray with given sum | Set 2 (Handles Negative Numbers)
- Sort an array without changing position of negative numbers
- Pairs of Positive Negative values in an array
- Rearrange array in alternating positive & negative items with O(1) extra space | Set 2
- Rearrange array in alternating positive & negative items with O(1) extra space | Set 1
- Check whether two numbers are in silver ratio
- Kth Smallest sum of continuous subarrays of positive numbers
- Python program to print all negative numbers in a range
- Python program to print negative numbers in a list
- Find Kth number from sorted array formed by multiplying any two numbers in the array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

