- 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

We are given an array let’s say, arr[] of integer values and the task is to calculate the count of array elements that divides the sum of all other elements.

Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Input− int arr_1[] = {9, 6, 3}Output− count is 3

**Explanation** − As the sum of elements 9+6 = 15 which is divisible by 3, sum of elements 9+3 =12 which is divisible by 6 and 6+3 = 9 which is divisible by 9. Therefore, count is 3.

Input− arr[] = {3, 10, 4, 6, 7}Output− count is 3

**Explanation** − As the sum of elements 10+4+6+7 = 27 which is divisible by 3, sum of elements 3+4+6+7 =20 which is divisible by 10 and 3+10+4+7 = 24 which is divisible by 6. Therefore, count is 3.

Create an array let’s say, arr[]

Calculate the length of an array using the length() function that will return an integer value as per the elements in an array.

Take a temporary variable that will store the count of elements.

Start loop for i to 0 and i less than size of array

Inside the loop, set temporary variable let’s say temp to 0

Inside the loop, start another loop for j to 0 and j less than size of an array

Check if i = j then continue

Else, set temp = temp + arr[j]

Now check if temp % arr[i] =0 then increment the value of count by 1

Return the count

Print the result.

#include <iostream> using namespace std; int countelements( int arr_1[], int size){ // To store the count of required numbers int result = 0; for (int i = 0; i < size; i++){ // Initialize sum to 0 int sum = 0; for (int j = 0; j < size; j++){ if (i == j){ continue; } else{ sum += arr_1[j]; } } // If sum is divisible by the chosen element if (sum % arr_1[i] == 0){ result++; } } // Return the count return result; } // main function int main(){ int arr_1[] = { 1, 2, 3, 4, 5, 6 }; int size = sizeof(arr_1) / sizeof(arr_1[0]); cout <<"count is " <<countelements(arr_1, size); return 0; }

If we run the above code we will get the following output −

count is 2

- Related Questions & Answers
- Count of elements whose absolute difference with the sum of all the other elements is greater than k in C++
- Divide every element of one array by other array elements in C++ Program
- Count of arrays in which all adjacent elements are such that one of them divide the another in C++
- Count and Sum of composite elements in an array in C++
- Count frequencies of all elements in array in Python
- Compute sum of all elements in 2 D array in C
- Count numbers in a range that are divisible by all array elements in C++
- Sum of all the non-repeating elements of an array JavaScript
- How to find the sum of all array elements in R?
- Minimizing array sum by applying XOR operation on all elements of the array in C++
- Maximum possible sum of a window in an array such that elements of same window in other array are unique in c++
- Sum all similar elements in one array - JavaScript
- Count number of elements between two given elements in array in C++
- Elements to be added so that all elements of a range are present in array in C++
- Print array elements that are divisible by at-least one other in C++

Advertisements