# Array Range Queries to find the Maximum Armstrong number with updates

The Array range queries are a new interest of data structure. In this query, we have set the random element to the array and given the general query problem to solve the data structure problem efficiently. Armstrong number is the total of its digits' cubes. For example- 0, 1, 153, 370, 371, and 407 are Armstrong numbers.

Let’s take an example to understand the Armstrong number

Example 1 − The given number is 371 and check whether the number is Armstrong or not.

3*3*3 + 7*7*7 + 1*1*1 = 371

Hence, this is Armstrong number.

Example 2 − The given number is 121 and check whether the number is Armstrong or not.

1*1*1 + 2*2*2 + 1*1*1 = 9

Hence, this is not an Armstrong number.

In this article, we are going to solve the array range queries to find the Maximum Armstrong number with updates.

## Syntax

Vector<object_type> variable_name;


This is a way to declare the vector in the program.

## Algorithm

• We are creating a function definition named “isArmstrong” which n as a parameter to check whether the number is Armstrong or not.

The following point to understand the operation of Armstrong Number −

• Store the value ‘0’ to the ‘sum’ variable that will later meet for the addition of each number having power.

• Then store ‘n’ in the variable ‘temp’. This temporary variable will be used in the while loop to check for the condition of armstrong number.

• Next, we are storing the value ‘0’ in the variable ‘digits’ that will later find the power of each number.

• Now start the main function and initialize the variable “arr[]” to set the given array element.

• We are printing the array element by using the first for loop.

• Initialize the vector variable named “armstrong” which will meet the condition in if-statement to find the list Armstrong number by using predefined function pushback().

• Then we are using second for loop to iterate the array length index and under this loop, the if-else statement has used to find the list of array elements based on Armstrong or Non-Armstrong number.

• To update the array range queries, we are initializing the variable named ‘newNumber’ to store the new array element that will check for verification of Armstrong number or not by using an if-else statement.

• Next, store 0 to the variable ‘maxArmstrong’ that keeps track of the maximum armstrong number in array elements.

• Moving ahead to use the third for loop, it iterates the Armstrong element length. Inside this loop, the if statement has used to find the maximum Armstrong number.

• Then use the last for loop to iterate the following array element satisfied for Armstrong number and print all Armstrong number.

• Finally, we are printing the maximum Armstrong number with the help of ‘maxArmstrong’ variable.

## Example

In this program, we are going to find the maximum Armstrong number with updates.

#include <bits/stdc++.h>
using namespace std;
// Function to check if a number is an Armstrong number or not
bool isArmstrong(int n) {
int sum = 0;
int temp = n;
int digits = 0;
while (temp > 0) {
digits++;
temp /= 10;
}
temp = n;
while (temp > 0) {
int digit = temp % 10;
sum += pow(digit, digits);
temp /= 10;
}
return sum == n;
}
int main() {
int arr[] = {0, 123, 1, 19, 12, 153, 370};
int a = sizeof(arr) / sizeof(arr);
cout<<"The given array element:";
for(int m = 0; m < a; m++) {
cout<<arr[m]<<" ";
}
// Vector to store Armstrong numbers
vector<int> armstrongs;
// Check each element of the array if it's an Armstrong number or not

cout<<"\nThe element found to be Non-Armstrong number\n";
for (int i = 0; i < a; i++) {
if (isArmstrong(arr[i])) {
armstrongs.push_back(arr[i]);
} else {
cout << arr[i] << endl;
}
}
// Add a new number to the array and check if it's an Armstrong number or not
int newNumber = 1278;
if (isArmstrong(newNumber)) {
cout << " : Armstrong number" << endl;
armstrongs.push_back(newNumber);
} else {
cout << " : Non-Armstrong number" << endl;
}
// Find the maximum Armstrong number in the array
int maxArmstrong = 0;
for (int i = 0; i < armstrongs.size(); i++) {
if (armstrongs[i] > maxArmstrong) {
maxArmstrong = armstrongs[i];
}
}
cout << "The following array element satisfied for Armstrong Number: ";
for (int i = 0; i < armstrongs.size(); i++) {
cout << armstrongs[i] << " ";
}
cout << endl;
cout << "The maximum Armstrong number in the array is: " << maxArmstrong << endl;
return 0;
}


## Output

There are 3 array element whose setbits are in a multiple of KThe given array element:0 123 1 19 12 153 370
The element found to be Non-Armstrong number
123
19
12
The newly added number	1278 : Non-Armstrong number
The following array element satisfied for Armstrong Number: 0 1 153 370
The maximum Armstrong number in the array is: 370


## Conclusion

We explored the concept of array range queries to find the maximum Armstrong number with updates. We saw how given array elements are filtered into the combination of Armstrong and Non-Armstrong number. After removing the non-Armstrong number from existing array elements, we simply printed the result of satisfied array elements of Armstrong type and find the maximum among them.

Updated on: 10-May-2023

230 Views 