Table of Contents
Example
method 1
algorithm
Output
Method 2
in conclusion
Home Backend Development C++ Checks whether the number formed by concatenating array elements is a hashed number

Checks whether the number formed by concatenating array elements is a hashed number

Aug 25, 2023 pm 08:17 PM
connect array elements Hash number

Checks whether the number formed by concatenating array elements is a hashed number

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;
}
Copy after login

Output

Yes, the number formed by concatenating the array element is a Harshad number
Copy after login

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;
}
Copy after login

Output

No, the number formed by concatenating the array element is not a Harshad number
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Complete Guide to Win11 Gamepad Connection Complete Guide to Win11 Gamepad Connection Jan 02, 2024 pm 05:26 PM

After updating to the latest win11 system, many users are not sure how to connect the controller to play games. For this reason, we have brought you a detailed tutorial on connecting the win11 controller today. If you haven't completed the connection yet, let's take a look at how to operate it. . How to connect the controller in win11: 1. Click Start below and then enter Windows Settings to open the "Control Panel". 2. After entering, you can find "View devices and printers" to enter. 3. At this point you can see the information about the controller device and just make the connection. 4. After the connection is successful, a √ appears, and the connection is completed.

win7 printer cannot connect error code 0x0000011b win7 printer cannot connect error code 0x0000011b Dec 26, 2023 pm 10:01 PM

Many times we need to use a computer to connect to the printer for various printing operations, but sometimes some users will encounter the problem that win7 cannot connect to the printer 0x0000011b. The following is the specific solution. win7 cannot connect to the printer 0x0000011b1. Shortcut key "win+r", enter "regedit" 2. Find the following path "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print" 3. Right-click to create a new value "DWORD (32-bit) value (D) and Set the value to 0. Name the new project "RpcAuthnLevelPri

How to check network connection details and status on Windows 11 How to check network connection details and status on Windows 11 Sep 11, 2023 pm 02:17 PM

In order to make sure your network connection is working properly or to fix the problem, sometimes you need to check the network connection details on Windows 11. By doing this, you can view a variety of information including your IP address, MAC address, link speed, driver version, and more, and in this guide, we'll show you how to do that. How to find network connection details on Windows 11? 1. Use the "Settings" app and press the + key to open Windows Settings. WindowsI Next, navigate to Network & Internet in the left pane and select your network type. In our case, this is Ethernet. If you are using a wireless network, select a Wi-Fi network instead. At the bottom of the screen you should see

How to connect keep body fat scale How to connect keep body fat scale Mar 07, 2024 pm 04:50 PM

How to connect the keep body fat scale? Keep has a specially designed body fat scale, but most users do not know how to connect the keep body fat scale. Next is the graphic tutorial on the connection method of the keep body fat scale that the editor brings to users. , interested users come and take a look! How to connect the keep body fat scale 1. First open the keep software, go to the main page, click [My] in the lower right corner, and select [Smart Hardware]; 2. Then on the My Smart Devices page, click the [Add Device] button in the middle; 3 , then select the device you want to add interface, select [Smart Body Fat/Weight Scale]; 4. Then on the device model selection page, click the [keep body fat scale] option; 5. Finally, in the interface shown below, finally [Add Now] at the bottom

How to solve limited network connection in Windows 10 How to solve limited network connection in Windows 10 Feb 19, 2024 pm 09:20 PM

Solutions to Restricted Network Connections in Win10 With the rapid development of technology, the Internet has become an indispensable part of people's lives. However, sometimes we may encounter some problems when connecting to the Internet on computers using the Windows 10 operating system, one of which is restricted connections. In this case, we cannot access web pages, download files, or use network functions normally. So, is there any way to solve this problem? This article will introduce you to several common solutions. 1. Check the network connection settings. First, I

What does printer error code 0x0000011b mean? What does printer error code 0x0000011b mean? Feb 18, 2024 pm 10:02 PM

What does 0x0000011b mean when connecting to a printer? Users often encounter various error codes when using computers, laptops or other devices. Among them, 0x0000011b is a common printer connection error code. So, what does connecting printer 0x0000011b mean? First, we need to understand the basic principles of printer connection. When we need to print files from the computer, we usually need to connect the printer to the computer for data transfer between the two. This connection can be made via

Shared printer cannot connect to printer Shared printer cannot connect to printer Feb 22, 2024 pm 01:09 PM

With the development of the digital era, shared printers have become an indispensable part of the modern office environment. However, sometimes we may encounter the problem that the shared printer cannot be connected to the printer, which will not only affect work efficiency, but also cause a series of troubles. This article aims to explore the reasons and solutions for why a shared printer cannot connect to the printer. There are many reasons why a shared printer cannot connect to the printer, the most common of which is network issues. If the network connection between the shared printer and the printer is unstable or interrupted, normal operation will not be possible.

How to connect OnePlus watch to Bluetooth headset_How to connect OnePlus watch to Bluetooth headset How to connect OnePlus watch to Bluetooth headset_How to connect OnePlus watch to Bluetooth headset Mar 23, 2024 pm 01:16 PM

1. Place the earphones in the earphone box and keep the lid open. Press and hold the button on the box to enter the pairing state of the earphones. 2. Turn on the watch music function and select Bluetooth headphones, or select Bluetooth headphones in the watch settings function. 3. Select the headset on the watch to pair successfully.

See all articles