The k-sorting function of an array is defined as follows:
k
distinct indices from the array.1
to the elements at the chosen indices.Now, given an array nums
and an integer k
, return true
if the array can be made non-decreasing by applying a k-sorting function exactly once, or false
otherwise.
Example 1:
Input: nums = [0,1,2,4,6,5,3] Output: true Explanation: By choosing the indices[3, 5, 6]
, we can increase the values at these indices by 1, which turnsnums
into[0,1,2,5,7,6,4]
. This results in the non-decreasing array[0,1,2,5,6,7,4]
.
Example 2:
Input: nums = [0,1,2,4,6,5,3] Output: false Explanation: It is not possible to make the array non-decreasing by applying a k-sorting function once.
Constraints:
1 <= nums.length <= 105
0 <= nums[i] <= 109
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find an Armstrong number the brute force way, we'll check every number within a given range to see if it meets the Armstrong criteria. For each number, we calculate the sum of its digits raised to the power of the number of digits, then compare the sum to the original number.
Here's how the algorithm would work step-by-step:
def find_armstrong_numbers(start_range, end_range):
armstrong_numbers = []
for current_number in range(start_range, end_range + 1):
number_string = str(current_number)
number_of_digits = len(number_string)
sum_of_powers = 0
# Iterate through digits to calculate the sum of powers
for digit_char in number_string:
digit = int(digit_char)
sum_of_powers += digit ** number_of_digits
# Verify if the number is an Armstrong number
if current_number == sum_of_powers:
# Add the Armstrong number to the list
armstrong_numbers.append(current_number)
return armstrong_numbers
def is_armstrong_number(number_to_check):
number_string = str(number_to_check)
number_of_digits = len(number_string)
sum_of_powers = 0
# Iterate through digits to calculate the sum of powers
for digit_char in number_string:
digit = int(digit_char)
sum_of_powers += digit ** number_of_digits
#Return if Armstrong number
return number_to_check == sum_of_powers
def get_armstrong_numbers_in_range(start_number, end_number):
list_armstrong_numbers = []
for number in range(start_number, end_number + 1):
# Check each number in range
if is_armstrong_number(number):
list_armstrong_numbers.append(number)
#Return the list of Armstrong numbers
return list_armstrong_numbers
An Armstrong number is a number where the sum of its digits, each raised to the power of the number of digits, equals the original number. To efficiently check if a number is an Armstrong number, we need a two-step process. First determine the digit count, and then sum the digits raised to that power, finally compare the sum with the original number.
Here's how the algorithm would work step-by-step:
def is_armstrong_number(number):
number_string = str(number)
number_of_digits = len(number_string)
# Need digit count for raising each digit to its power
sum_of_powers = 0
for digit_char in number_string:
digit = int(digit_char)
sum_of_powers += digit ** number_of_digits
# Compare sum with original number
if sum_of_powers == number:
return True
else:
return False
def main():
test_number_one = 153
test_number_two = 120
# Test case to confirm functionality
is_armstrong_one = is_armstrong_number(test_number_one)
is_armstrong_two = is_armstrong_number(test_number_two)
print(f'{test_number_one} is Armstrong: {is_armstrong_one}')
print(f'{test_number_two} is Armstrong: {is_armstrong_two}')
if __name__ == "__main__":
main()
Case | How to Handle |
---|---|
Input is a negative number | Return false, since Armstrong numbers are defined for non-negative integers only. |
Input is zero | Return true, as 0 is considered an Armstrong number for a power of 1 (0^1 = 0). |
Single-digit number (0-9) | Return true, as any single-digit number is an Armstrong number (e.g., 5^1 = 5). |
Large integer input leading to potential overflow during power calculation | Use a data type that supports larger numbers or check for overflow before returning the result, potentially returning false in case of overflow. |
Input number is extremely large (e.g., close to the maximum integer value) | Handle large input carefully by using appropriate data types and considering the performance implications of repeated exponentiation. |
Input contains only the digit '1' | The solution should correctly handle this, as the sum of powers will equal the original number. |
Input is a floating-point number or string | Cast it to an integer or return an error message/false since Armstrong numbers are integers. |
Integer overflow during the sum of powered digits | Use a larger data type (e.g., long) or check for overflow during each digit power summation to prevent inaccurate results. |