Max 2 integers from an array

def sol2(data):
    max_idx1 = 0
    max_idx2 = 1
    max_sum = data[0] + data[1]

    for next_idx in range(2, len(data)):
        
        if data[max_idx1] + data[next_idx] > max_sum and max_idx1 != next_idx:
            max_sum = data[max_idx1] + data[next_idx]
            max_idx2 = next_idx

        if data[max_idx2] + data[next_idx] > max_sum and max_idx2 != next_idx:
            max_sum = data[max_idx2] + data[next_idx]
            max_idx1 = next_idx

    print(max_idx1, max_idx2)


if __name__ == "__main__":
    data = [1, 5, 3, 2, 4]
    sol2(data)
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s