Under construction

Still being built. Things may change

Stock Market

Interview

Time limit1sMemory limit256 MB

Summary
Find the contiguous subarray with the largest sum and report its 1-based start and end indices, breaking ties by smallest start then smallest end.
Level

Medium5 of 10

Topics
Dynamic programming, Greedy
Solved
No attempts yet

Problem

A bank has collected day-to-day data of the daily profit (or loss) of the shares it holds. Using these numbers, it wants to determine on which single day it should have bought and on which single day it should have sold in order to maximize its profit, so it can compare that ideal outcome with its actual performance.

Given the sequence of daily profits, write a program that finds the contiguous run whose total profit is largest. The run is described by the 1-based indices of its first and last elements (indices start at 1). Exactly one buy day and one sell day must be chosen. (Otherwise the task would be trivial: simply hold the shares on every day whose profit is non-negative.)

Input

The first line contains the number of test cases. Each test case has the following format.

  • One line with the length of the sequence NN (1≤N≤1061 \le N \le 10^6).
  • One line with NN integers pip_i separated by single spaces (−103≤pi≤103-10^3 \le p_i \le 10^3), where pip_i is the profit (or loss) on day ii. At least one of the integers is positive.

Output

For each test case, print on a single line two integers ii and jj (1≤i≤j≤N1 \le i \le j \le N) such that the sum of the ii-th through jj-th integers (inclusive) is maximized. If several pairs achieve the maximum, print the one with the smallest ii; if there is still a tie, print the one with the smallest jj.

Examples3

  1. Example 1

    Input
    2
    11
    -3 1 -1 2 3 1 -1 2 -3 -5 7
    9
    1 -2 3 -1 -1 3 -2 2 -4
    
    Expected output
    2 8
    3 6
    
  2. Example 2

    Input
    1
    1
    1
    
    Expected output
    1 1
    
  3. Example 3

    Input
    1
    5
    1 2 3 4 5
    
    Expected output
    1 5