

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ code to count numbers after division elements greater than half of array size
<p>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.</p><p>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.</p><h2>Steps</h2><p>To solve this, we will follow these steps −</p><pre class="just-code notranslate language-cpp" data-lang="cpp">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</pre><h2>Example</h2><p>Let us see the following implementation to get better understanding −</p><pre class="demo-code notranslate language-cpp" data-lang="cpp">#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; }</pre><h2>Input</h2><pre class="result notranslate">{ 10, 0, -7, 2, 6 }</pre><h2>Output</h2><pre class="result notranslate">1</pre>
- Related Questions & Answers
- Python Indices of numbers greater than K
- Count subarrays with all elements greater than K in C++
- Reduce Array Size to The Half in C++
- Mask array elements greater than a given value in Numpy
- Code Division Multiplexing
- Query for documents where array size is greater than 1 in MongoDB?
- Retaining array elements greater than cumulative sum using reduce() in JavaScript
- Count natural numbers whose all permutation are greater than that number in C++
- Mask array elements greater than or equal to a given value in Numpy
- Find the number of elements greater than k in a sorted array using C++
- Count of elements whose absolute difference with the sum of all the other elements is greater than k in C++
- Count of alphabets having ASCII value less than and greater than k in C++
- Count of Smaller Numbers After Self in C++
- C++ code to find xth element after removing numbers
- Count elements such that there are exactly X elements with values greater than or equal to X in C++
Advertisements