# C++ code to count numbers after division elements greater than half of array size

Suppose we have an array A with n elements. We have to find some non-zero integer d, such that such that, after each number in the array is divided by d, the number of positive values that are presented in the array is greater than or equal to the half of the array size. If there are multiple values of d that satisfy the condition. If there are multiple answers, return any one of them.

So, if the input is like A = [10, 0, -7, 2, 6], then the output will be 4, because here n = 5 , so we need at least $\mathrm{\left \lceil 5/2\right \rceil=3}$ elements after division. If d = 4, the array after division will be [2.5, 0, −1.75, 0.5, 1.5], in which there are 3 positive numbers are 2.5, 0.5 and 1.5.

## Steps

To solve this, we will follow these steps −

z := 0, f := 0
n := size of A
for initialize i := 0, when i < n, update (increase i by 1), do:
a := A[i]
if a > 0, then:
(increase z by 1)
if a < 0, then:
(increase f by 1)
if 2 * z >= n, then:
return 1
otherwise when 2 * f >= n, then:
return -1
Otherwise
return 0

## Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
int solve(vector<int> A){
int z = 0, f = 0;
int n = A.size();
for (int i = 0; i < n; i++){
int a = A[i];
if (a > 0)
z++;
if (a < 0)
f++;
}
if (2 * z >= n)
return 1;
else if (2 * f >= n)
return -1;
else
return 0;
}
int main(){
vector<int> A = { 10, 0, -7, 2, 6 };
cout << solve(A) << endl;
}

## Input

{ 10, 0, -7, 2, 6 }

## Output

1