Given an integer n
, return any array containing n
unique integers such that they add up to 0
.
Example 1:
Input: n = 5 Output: [-7,-1,1,3,4] Explanation: These arrays also are accepted [-5,-1,1,2,3] , [-3,-1,2,-2,4].
Example 2:
Input: n = 3 Output: [-1,0,1]
Example 3:
Input: n = 1 Output: [0]
Constraints:
1 <= n <= 1000
program main
implicit none
integer :: n
integer, allocatable :: arr(:)
call get_arguments(n)
allocate(arr(n))
arr = solve(n)
write(*,*) 'Solution:'
write(*,*) arr
contains
function solve(n) result(arr)
implicit none
integer, value :: n
integer, dimension(:), allocatable :: arr
integer :: i
do i = 1, n
arr(i) = -i
end do
end function solve
subroutine get_arguments(n)
implicit none
integer, intent(out) :: n
character(len=100) :: buffer
integer :: status
read(*,fmt='(A)',iostat=status) buffer
if (status /= 0) then
stop 'Error reading input'
end if
read(buffer,fmt=*) n
end subroutine get_arguments
end program main
STOP Error reading input
! Declare variables
integer :: n, i, j, k
integer, allocatable :: arr(:)
! Read input
read(*,*) n
! Initialize array
allocate(arr(n))
! Fill array with integers
do i = 1, n
arr(i) = i - 1
end do
! Check if array adds up to 0
do i = 1, n
do j = i + 1, n
if (sum(arr(i:j)) == 0) then
write(*,*) arr(i:j)
exit
end if
end do
end do
end
At line 6 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x78da03226960 in ??? #1 0x78da032274d9 in ??? #2 0x78da0347b17b in ??? #3 0x78da03474684 in ??? #4 0x78da034752aa in ??? #5 0x59b166a7e272 in MAIN__ #6 0x59b166a7e5ce in main
from heapq import heappush, heappop
def longestHappyString(a, b, c):
res = ""
pq = []
if a: heappush(pq, (-a, 'a'))
if b: heappush(pq, (-b, 'b'))
if c: heappush(pq, (-c, 'c'))
while pq:
curr = heappop(pq)
if len(res) >= 2 and res[-1] == curr[1] and res[-2] == curr[1]:
if not pq: break
next = heappop(pq)
res += next[1]
if next[0] < -1: heappush(pq, (next[0] + 1, next[1]))
heappush(pq, curr)
else:
res += curr[1]
if curr[0] < -1: heappush(pq, (curr[0] + 1, curr[1]))
return res
The algorithm uses a priority queue to track the count of each character, sorted in descending order. It then generates the happy string by greedily appending the character with the highest count, as long as it does not violate the "no three consecutive characters" rule. If this rule would be violated, it appends the character with the next highest count and puts the current character back in the queue to try later. This process is continued until the queue is empty, and the resulting string is the longest happy string.
#include <string>
#include <queue>
std::string longestHappyString(int a, int b, int c) {
std::string res;
using CharCount = std::pair<int, char>;
std::priority_queue<CharCount> pq;
if (a) pq.push({a, 'a'});
if (b) pq.push({b, 'b'});
if (c) pq.push({c, 'c'});
while (!pq.empty()) {
auto curr = pq.top(); pq.pop();
if (res.size() >= 2 && res[res.size() - 1] == curr.second && res[res.size() - 2] == curr.second) {
if (pq.empty()) break;
auto next = pq.top(); pq.pop();
res += next.second;
if (--next.first > 0) pq.push(next);
pq.push(curr);
} else {
res += curr.second;
if (--curr.first > 0) pq.push(curr);
}
}
return res;
}
The algorithm uses a priority queue to track the count of each character, sorted in descending order. It then generates the happy string by greedily appending the character with the highest count, as long as it does not violate the "no three consecutive characters" rule. If this rule would be violated, it appends the character with the next highest count and puts the current character back in the queue to try later. This process is continued until the queue is empty, and the resulting string is the longest happy string.