


In this question, we are given an array of integers. We need to combine all the elements into an integer and check if it is a Harshad number.
Before we move on to the solution, let us understand the Harshad number. All numbers are Harshad numbers, which are divisible by the sum of their numbers. For example, 12 is Harshad's number because 12 is divisible by 3, which is the sum of 1 2.
To solve this problem, we can add all the array elements and then check whether the result is a Harshad number.
Problem Statement - We are given an array of integers. We need to combine all the elements into a number and check if the combined number is a Harshad number.
Example
Input– arr = {1, 35, 69, 60};
Output-Yes
Explanation - The resulting number 1356960 is divisible by its sum.
Input – arr = {1, 65, 78, 1}
Output – No
Note – The combined number 165781 is not divisible by 28.
Input – arr = {1, 44}
Output-Yes
Explanation——144 is divisible by 9.
method 1
This method combines all array elements into a string. We will then use the stoi() method to convert the combined strings into integers. Afterwards, we can use the modulo operator to check if a number is divisible by the sum of its digits.
algorithm
Define a "combined" string variable and initialize it with an empty string.
Iterate over an array of integers. Convert a number to a string using the to_string() method. After that, append it to the "combined" variable.
Define the variable 'sum' and initialize it to zero to store the sum of numbers.
Loop through the combined string and store the sum of each number.
Use the stoi() method to convert the combined string to an integer. Afterwards, the integer is taken modulo and a Boolean value is returned based on the result.
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), since we iterate over the string.
Space Complexity - O(1) since we don't use extra space.
Method 2
In this method, we will perform the modulo operation on each small block of the combined integer and check whether the large integer is divisible by its sum.
algorithm
Define "combined" string variables.
Iterate over the integer array and store all integer combinations into the 'combined' variable.
Store the sum of numbers in the "sum" variable
Use a loop to iterate over the "combined" string.
Define the 'current' variable and initialize it to zero
Multiply the 'current' variable by 10 and add the current numeric value. Then, store the resulting value in the 'current' variable.
Perform modulo operation on ‘current’ and sum.
When all iterations of the loop are completed, return true if the value of the "current" variable is zero. Returns false if the current variable's value is non-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)
in conclusion
We learned two different ways to solve the problem. The first method is only used when the array contains fewer elements, since the stoi() method has some limitations when converting strings to integers. The second method is general and can be used for N array elements.
The above is the detailed content of Checks whether the number formed by concatenating array elements is a hashed number. For more information, please follow other related articles on the PHP Chinese website!

win11无法连接局域网打印机怎么办?有不少用户的win11系统在使用局域网打印机功能的时候,无法正常连接到共享打印机,出现这个问题,可能是连接步骤或者是某项功能服务没打开。今天小编就给大家带来了多种解决方法,很多小伙伴不知道怎么详细操作,小编下面整理了win11电脑不能连共享打印机解决技巧,如果你感兴趣的话,跟着小编一起往下看看吧!win11电脑不能连共享打印机解决技巧1、首先,按键盘上的Win+X组合键,或右键点击任务栏上的Windows开始图标,在打开的隐藏菜单项中,选择运行;2、运行窗口

MariaDB是一种开源的关系型数据库管理系统,它是MySQL的一个分支。PHP作为一种开源的服务器端脚本语言,被广泛应用于Web开发中。在很多Web开发项目中,需要使用PHP连接到MariaDB数据库,以便在Web应用程序中存储和检索数据。这篇文章将介绍如何使用PHP编写代码来连接MariaDB数据库。一、安装MariaDB服务器在使用PHP连接Maria

随着现代程序开发的不断发展,软件项目往往需要同时使用多个任务库来处理不同的任务。PHP语言一直是Web开发的重要工具,其在7.0版本中引入了新的并发处理特性,使得PHP在处理任务库时更加高效、灵活。在8.0版本中,PHP又新增了对连接多个任务库的支持,这为我们在处理大量数据和并发请求时提供了极大的便利。在本文中,我们将探讨PHP8.0中连接多个任务库的方法和

MySQL连接错误1203,如何解决?MySQL是一种广泛使用的关系型数据库管理系统,但是在使用MySQL时,一些连接错误可能会出现。其中一个常见的错误是错误代码1203,它表示数据库连接已中断。在遇到这个错误时,可以采取一些措施来解决问题。首先,我们需要确定错误1203的确切原因。这个错误通常是由于连接超时或连接过多引起的。连接超时可能是由于数据库服务器负

Python连接阿里云接口,实现邮件发送功能阿里云提供了一系列的服务接口,其中包括了邮件发送服务。通过Python脚本连接阿里云接口,我们可以实现邮件的快速发送。本篇文章将向您展示如何使用Python脚本连接阿里云接口,并实现邮件发送功能。首先,我们需要在阿里云上申请邮件发送服务,获取相应的接口信息。在阿里云管理控制台中,选择邮件推送服务,然后创建一个新的邮

MySQL连接错误1062,如何解决?MySQL是一种常用的关系型数据库管理系统,广泛应用于各种软件开发和数据存储场景中。在使用MySQL的过程中,我们有时会遇到各种错误,其中一个常见的错误是连接错误1062。本文将介绍这个错误的原因以及解决方法。首先,我们需要了解连接错误1062的含义。连接错误1062通常意味着在执行插入操作时,违反了表的唯一约束条件。在

如何在Java程序中优化MySQL连接的写入性能和并发性能?在开发Java程序时,我们经常需要使用到数据库,而MySQL作为一种常见的数据库管理系统,其连接的写入性能和并发性能是我们需要关注的重点。本文将介绍如何在Java程序中优化MySQL连接的写入性能和并发性能,以提升程序的效率。使用连接池管理连接:连接池可以管理数据库连接的创建、销毁和复用,避免频繁地

MySQL连接错误2059,该如何解决?在使用MySQL数据库时,有时会遇到连接错误2059的问题。这个错误通常是由于MySQL服务器与客户端之间的连接问题引起的。在本文中,我将解释导致此错误的一些常见原因,并提供一些可能的解决方案。防火墙问题:在一些情况下,防火墙设置可能会阻止MySQL服务器与客户端之间的通信。这种情况下,你需要检查防火墙设置并确保MyS


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version
God-level code editing software (SublimeText3)
