You are given a positive integer num
. You may swap any two digits of num
that have the same parity (i.e. both odd digits or both even digits).
Return the largest possible value of num
after any number of swaps.
Example 1:
Input: num = 1234 Output: 3412 Explanation: Swap the digit 3 with the digit 1, this results in the number 3214. Swap the digit 2 with the digit 4, this results in the number 3412. Note that there may be other sequences of swaps but it can be shown that 3412 is the largest possible number. Also note that we may not swap the digit 4 with the digit 1 since they are of different parities.
Example 2:
Input: num = 65875 Output: 87655 Explanation: Swap the digit 8 with the digit 6, this results in the number 85675. Swap the first digit 5 with the digit 7, this results in the number 87655. Note that there may be other sequences of swaps but it can be shown that 87655 is the largest possible number.
Constraints:
1 <= num <= 109
program main
implicit none
integer :: num
print '(A)', solve(1234)
print '(A)', solve(65875)
contains
function solve(num) result(max_value)
implicit none
integer, intent(in) :: num
integer :: max_value
integer :: i, j, temp
do i = 1, len(num) - 1
do j = i + 1, len(num)
if (mod(num(i), 2) == mod(num(j), 2)) then
temp = num(i)
num(i) = num(j)
num(j) = temp
end if
end do
end do
max_value = num
end function solve
end program main
temp.f95:27:23: 27 | max_value = num | 1 Error: Function ‘num’ requires an argument list at (1) temp.f95:11:22: 11 | function solve(num) result(max_value) | 1 Error: PROCEDURE attribute conflicts with INTENT attribute in ‘num’ at (1) temp.f95:21:20: 21 | num(i) = num(j) | 1 Error: The function result on the lhs of the assignment at (1) must have the pointer attribute. temp.f95:22:20: 22 | num(j) = temp | 1 Error: The function result on the lhs of the assignment at (1) must have the pointer attribute. temp.f95:18:30: 18 | do j = i + 1, len(num) | 1 Error: ‘string’ argument of ‘len’ intrinsic at (1) must be CHARACTER temp.f95:17:22: 17 | do i = 1, len(num) - 1 | 1 Error: ‘string’ argument of ‘len’ intrinsic at (1) must be CHARACTER temp.f95:6:23: 6 | print '(A)', solve(1234) | 1 Error: Expected a procedure for argument ‘num’ at (1) temp.f95:7:23: 7 | print '(A)', solve(65875) | 1 Error: Expected a procedure for argument ‘num’ at (1)
PROGRAM LARGEST_NUMBER
IMPLICIT NONE
INTEGER, PARAMETER :: MAX_NUM = 109
INTEGER :: num, max_num, i, j, temp
READ(*,*) num
max_num = num
DO i = 1, MAX_NUM
DO j = i + 1, MAX_NUM
IF (MOD(num, i) == MOD(num, j)) THEN
temp = num / 10**(i-1)
temp = temp - (temp / 10) * 10
temp = temp + (temp / 10) * 10
num = num - temp * 10**(i-1) + temp * 10**(j-1)
max_num = MAX(max_num, num)
END IF
END DO
END DO
WRITE(*,*) max_num
END PROGRAM LARGEST_NUMBER
This script uses a brute force approach to solve the problem. It iterates through all possible swaps of digits in the input number, and checks if the resulting number is larger than the current largest number. If it is, it updates the largest number. The script uses the `MOD` function to check if two digits have the same parity, and the `MAX` function to compare and update the largest number.
The script uses the `IMPLICIT NONE` statement to disable the use of implicit typing, and the `PARAMETER` statement to define a constant for the maximum value of the input number.
The script reads the input number from the standard input, and stores it in the `num` variable. It then initializes the `max_num` variable to the input number, and starts the brute force search for the largest possible number.
The script uses a nested loop to iterate through all possible swaps of digits in the input number. The outer loop iterates through the digits of the input number, and the inner loop iterates through the remaining digits of the input number. The `IF` statement checks if the two digits have the same parity, and if they do, it swaps them and updates the `num` variable. The `MAX` function is used to compare and update the largest number.
The script writes the largest possible number to the standard output.
Note that this script assumes that the input number is a positive integer between 1 and 109, inclusive. If the input number is outside of this range, the script will not work correctly.
temp.f95:5:29: 5 | INTEGER :: num, max_num, i, j, temp | 1 Error: Symbol ‘max_num’ at (1) already has basic type of INTEGER temp.f95:27:132: 27 | This script uses a brute force approach to solve the problem. It iterates through all possible swaps of digits in the input number, and checks if the resulting number is larger than the current largest number. If it is, it updates the largest number. The script uses the `MOD` function to check if two digits have the same parity, and the `MAX` function to compare and update the largest number. | 1 Error: Line truncated at (1) [-Werror=line-truncation] temp.f95:29:132: 29 | The script uses the `IMPLICIT NONE` statement to disable the use of implicit typing, and the `PARAMETER` statement to define a constant for the maximum value of the input number. | 1 Error: Line truncated at (1) [-Werror=line-truncation] temp.f95:31:132: 31 | The script reads the input number from the standard input, and stores it in the `num` variable. It then initializes the `max_num` variable to the input number, and starts the brute force search for the largest possible number. | 1 Error: Line truncated at (1) [-Werror=line-truncation] temp.f95:33:132: 33 | The script uses a nested loop to iterate through all possible swaps of digits in the input number. The outer loop iterates through the digits of the input number, and the inner loop iterates through the remaining digits of the input number. The `IF` statement checks if the two digits have the same parity, and if they do, it swaps them and updates the `num` variable. The `MAX` function is used to compare and update the largest number. | 1 Error: Line truncated at (1) [-Werror=line-truncation] temp.f95:37:132: 37 | Note that this script assumes that the input number is a positive integer between 1 and 109, inclusive. If the input number is outside of this range, the script will not work correctly. | 1 Error: Line truncated at (1) [-Werror=line-truncation] temp.f95:11:10: 11 | DO i = 1, MAX_NUM | 1 Error: Symbol ‘i’ at (1) has no IMPLICIT type temp.f95:12:13: 12 | DO j = i + 1, MAX_NUM | 1 Error: Symbol ‘j’ at (1) has no IMPLICIT type temp.f95:7:19: 7 | READ(*,*) num | 1 Error: Symbol ‘num’ at (1) has no IMPLICIT type temp.f95:14:19: 14 | temp = num / 10**(i-1) | 1 Error: Symbol ‘temp’ at (1) has no IMPLICIT type f951: some warnings being treated as errors
def first_palindrome(words):
for word in words:
if word == word[::-1]:
return word
return ""
The algorithm used in each function iterates through the given array of strings, and for each string, it checks if it's a palindrome by comparing it to its reversed version. If a palindrome is found, it immediately returns that string. If no palindromes are found after iterating through the entire array, an empty string is returned. Each language's implementation follows similar logic, with some differences:
std::reverse()
function to reverse the string.StringBuilder
to reverse the string, and the equals()
method to compare the strings.split()
, reverse()
, and join()
methods to reverse the string.#include <string>
#include <vector>
std::string firstPalindrome(const std::vector<std::string>& words) {
for (const std::string& word : words) {
std::string reversedWord = word;
std::reverse(reversedWord.begin(), reversedWord.end());
if (word == reversedWord) {
return word;
}
}
return "";
}
The algorithm used in each function iterates through the given array of strings, and for each string, it checks if it's a palindrome by comparing it to its reversed version. If a palindrome is found, it immediately returns that string. If no palindromes are found after iterating through the entire array, an empty string is returned. Each language's implementation follows similar logic, with some differences:
std::reverse()
function to reverse the string.StringBuilder
to reverse the string, and the equals()
method to compare the strings.split()
, reverse()
, and join()
methods to reverse the string.