Jay
Jay 1
I've been stuck in this room for some time now. They locked me in here and told
me the only way I can leave is if I solve their problem within five seconds.
I've tried so much, but my algorithm keeps going over the time. Can you help me?
What I have to do is find the maximum contiguous sub-array within this array of
numbers. They keep telling me my algorithm isn't efficient enough! If you can
send me the indices of this array, I might live to see another day.
Format: "sum, i, j" Example: 103, 345, 455
sum = the maximum contiguous sum
i = start index of sub-array
j = end index of sub-array
Press Enter to get the arrThis is the maximum contiguous subarray algorithm. Luckily, I had experience with this algorithm (yay competitive programming!). The only twist is we had to output the start and end indices.
from pwn import *
conn = remote('chals3.umdctf.io', 6001)
conn.send('\n')
conn.recvuntil('[')
data = '[' + conn.recvuntil(']').decode()
print(data)
arr = eval(data)
max_so_far = 0
max_end_here = 0
curr_index = 0
curr_i = 0
i = 0
j = 0
while curr_index < len(arr):
max_end_here += arr[curr_index]
if max_end_here > max_so_far:
max_so_far = max_end_here
i = curr_i
j = curr_index
if max_end_here < 0:
max_end_here = 0
curr_i = curr_index + 1
curr_index += 1
print(f"{max_so_far}, {i}, {j}")
conn.send(f"{max_so_far}, {i}, {j}")
conn.recvline()
conn.recvline()
print(conn.recvline())
print(conn.recv())
conn.close()
Jay 2
The idea here is to apply the above algorithm for 1D arrays onto the 2D array, by treating the sum of each row of the subarea as an element in the 1D array. So, when finding the maximum contiguous sum of the "1D array", we are finding the optimal row_start and row_end for any combination of col_start and col_end,

Last updated
Was this helpful?