
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
- Related Articles
- Queries to count array elements greater than or equal to given number with updates
- Count subarrays with all elements greater than K in C++
- C++ Program to count of array elements greater than all elements on its left and at least K elements on its right
- Reduce Array Size to The Half in C++
- Count natural numbers whose all permutation are greater than that number in C++
- C++ code to count local extrema of given array
- Find the number of elements greater than k in a sorted array using C++
- Count elements such that there are exactly X elements with values greater than or equal to X in C++
- Count of elements whose absolute difference with the sum of all the other elements is greater than k in C++
- C++ code to find xth element after removing numbers
- Elements greater than the previous and next element in an Array in C++
- Find Array Elements Which are Greater than its Left Elements in Java?
- C++ code to count operations to make array sorted
- Count of Smaller Numbers After Self in C++
- C++ code to count number of lucky numbers with k digits
