- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if the number formed by concatenating all array elements is a Harshad number or not
In this problem, we have given the array of integers. We need to combine all elements in a single integer and check if it is a Harshad number.
Before we move with the solution, let’s understand Harshad number. All numbers are Harshad numbers which are divisible by the sum of their digits. For example, 12 is Harshad number, as 12 is divisible by 3 = 1 + 2.
To solve the problem, we can combine all array elements, and after that, we can check whether the resultant number is the Harshad number.
Problem statement – We have given an array of integers. We need to combine all elements into a single number and check whether the combiner number is the Harshad number.
Sample examples
Input – arr = {1, 35, 69, 60};
Output – YES
Explanation – The resultant number 1356960 is divisible by its sum.
Input – arr = {1, 65, 78, 1}
Output – No
Explanation – The combined number 165781 is not divisible by 28.
Input – arr = {1, 44}
Output – YES
Explanation – 144 is divisible by 9.
Approach 1
This approach will combine all array elements into a single string. After that, we will use the stoi() method to convert combined strings to integers. After that, we can use the modulo operator to check whether the number is divisible by the sum of its digits.
Algorithm
Define the ‘combined’ string variable and initialize it with an empty string.
Iterate through the array of integers. Use the to_string() method to convert a number to a string. After that, append it to the ‘combined’ variable.
Define the ‘sum’ variable and initialize it with zero to store the sum of digits.
Traverse the combined string, and store the sum of each digit.
Use the stoi() method to convert the combined string to an integer. After that, perform the modulo operation of an integer with the sum, and return a boolean value according to that.
Example
#include <iostream> #include <vector> using namespace std; // function to check whether the number formed by concatenating the elements of the array is a Harshad number or not bool isHarshadNumber(vector<int> array){ // store the concatenated number string combined = ""; // Iterate over the array for (auto num : array){ // Concatenate the string combined += to_string(num); } // Stores the sum of digits int sum = 0; // Calculate sum of digits for (int i = 0; i < combined.length(); i++) sum += (combined[i] - '0'); // Check if n is divisible by the sum return stoi(combined) % sum == 0; } int main(){ // Input vector<int> arr{1, 35, 69, 60}; if (isHarshadNumber(arr)) cout << "Yes, the number formed by concatenating the array element is a Harshad number"; else cout << "No, the number formed by concatenating the array element is not a Harshad number"; return 0; }
Output
Yes, the number formed by concatenating the array element is a Harshad number
Time complexity – O(N), as we traverse the string.
Space complexity – O(1), as we don’t use extra space.
Approach 2
In this approach, we will perform a modulo operation of every small chunk of a combined integer with a sum and check if the large integer is divisible by its sum.
Algorithm
Define the ‘combined’ string variable.
Iterate over the array of integers, combine all integers, and store them into the ‘combined’ variable.
Store the sum of digits in the ‘sum’ variable
Traverse the ‘combined’ string using the loop.
Define the ‘current’ variable and initialize with zero
Multiply the ‘current’ variable by 10, and add the current digit value. After that, store the resultant value in the ‘current’ variable.
Perform the modulo operation of ‘current’ with sum.
When all iterations of the loop are complete, return the true if the value of the ‘current’ variable is zero. Return false if the value of the current variable is other than zero.
Example
#include <iostream> #include <vector> using namespace std; // function to check whether the number formed by concatenating the elements of the array is a Harshad number or not bool isHarshadNumber(vector<int> array){ // store the concatenated number string combined = ""; // Iterate over the array for (auto num : array){ // Concatenate the string combined += to_string(num); } // Stores the sum of digits int sum = 0; // Calculate the sum of digits for (int i = 0; i < combined.length(); i++) sum += (combined[i] - '0'); // to store the current integer int current = 0; for (int i = 0; i < combined.size(); i++) { // Calculate the current integer by multiplying 10 and adding the current digit current = current * 10 + (combined[i] - '0'); // Check if the current integer is divisible by the sum current %= sum; } return current == 0; } int main(){ // Input vector<int> arr{1, 35, 69, 0}; if (isHarshadNumber(arr)) cout << "Yes, the number formed by concatenating the array element is a Harshad number"; else cout << "No, the number formed by concatenating the array element is not a Harshad number"; return 0; }
Output
No, the number formed by concatenating the array element is not a Harshad number
Time complexity – O(N)
Space complexity – O(1)
Conclusion
We learned two different approaches to solving the problem. The first approach is only used when an array contains fewer elements because the stoi() method has some limitations in converting a string to an integer. The second approach is universal, which can be used for N number of array elements.
To Continue Learning Please Login
Login with Google