Skip to main content
added 11 characters in body
Source Link
Bubbler
  • 79.3k
  • 5
  • 162
  • 484

K (ngn/k), 24 22 21 bytes

{t@'*<-/t:&x=/:x}0,+\

Try it online!

-1 byte thanks to coltim and Traws

Unfortunately(?), I found a 2-byte golf that bumps the time complexity to \$\mathcal{O}(n^2 \log n)\$ (and space complexity to \$\mathcal{O}(n^2)\$). This one utilizes "equality table" =/: and "deep where" & to extract all pairs of indices that represent zero-sum subarrays. Finding the pair that represents the longest such subarray is algorithmically the same.


K (ngn/k), 24 bytes

*<!/1-/'\(*'1|:\)'.=0,+\

Try it online!

Runs in \$\mathcal{O}(n \log n)\$ time and \$\mathcal{O}(n)\$ space. A monadic function train that takes a list of integers and returns [start index (inclusive), end index (exclusive)]. (For reading test case output, !0 and () are notations for empty lists, and ,0 is a notation for the singleton list [0].)

*<!/1-/'\(*'1|:\)'.=0,+\   monadic train; x: integer list
                    0,+\   prepend 0 to the cumulative sum of x
                   =       group; gives a dict of {value:indices}
                  .        extract values of the dict
                      any ordered pair inside each row forms a valid
                      (start,end) pair with subarray sum of zero
         (*'1|:\)'    for each list, extract (first,last)
 <!/1-/'\             sort this list by ascending order of (first-last)
*                     extract the first pair

It is actually shorter than a function that generates all contiguous subsequences and operates on them (which is \$\mathcal{O}(n^3)\$ time and space):

K (ngn/k), 26 bytes

{*(0=+/)#,/x{y'x}/:|!1+#x}

Try it online!

K (ngn/k), 24 22 21 bytes

{t@'*<-/t:&x=/:x}0,+\

Try it online!

-1 byte thanks to Traws

Unfortunately(?), I found a 2-byte golf that bumps the time complexity to \$\mathcal{O}(n^2 \log n)\$ (and space complexity to \$\mathcal{O}(n^2)\$). This one utilizes "equality table" =/: and "deep where" & to extract all pairs of indices that represent zero-sum subarrays. Finding the pair that represents the longest such subarray is algorithmically the same.


K (ngn/k), 24 bytes

*<!/1-/'\(*'1|:\)'.=0,+\

Try it online!

Runs in \$\mathcal{O}(n \log n)\$ time and \$\mathcal{O}(n)\$ space. A monadic function train that takes a list of integers and returns [start index (inclusive), end index (exclusive)]. (For reading test case output, !0 and () are notations for empty lists, and ,0 is a notation for the singleton list [0].)

*<!/1-/'\(*'1|:\)'.=0,+\   monadic train; x: integer list
                    0,+\   prepend 0 to the cumulative sum of x
                   =       group; gives a dict of {value:indices}
                  .        extract values of the dict
                      any ordered pair inside each row forms a valid
                      (start,end) pair with subarray sum of zero
         (*'1|:\)'    for each list, extract (first,last)
 <!/1-/'\             sort this list by ascending order of (first-last)
*                     extract the first pair

It is actually shorter than a function that generates all contiguous subsequences and operates on them (which is \$\mathcal{O}(n^3)\$ time and space):

K (ngn/k), 26 bytes

{*(0=+/)#,/x{y'x}/:|!1+#x}

Try it online!

K (ngn/k), 24 22 21 bytes

{t@'*<-/t:&x=/:x}0,+\

Try it online!

-1 byte thanks to coltim and Traws

Unfortunately(?), I found a 2-byte golf that bumps the time complexity to \$\mathcal{O}(n^2 \log n)\$ (and space complexity to \$\mathcal{O}(n^2)\$). This one utilizes "equality table" =/: and "deep where" & to extract all pairs of indices that represent zero-sum subarrays. Finding the pair that represents the longest such subarray is algorithmically the same.


K (ngn/k), 24 bytes

*<!/1-/'\(*'1|:\)'.=0,+\

Try it online!

Runs in \$\mathcal{O}(n \log n)\$ time and \$\mathcal{O}(n)\$ space. A monadic function train that takes a list of integers and returns [start index (inclusive), end index (exclusive)]. (For reading test case output, !0 and () are notations for empty lists, and ,0 is a notation for the singleton list [0].)

*<!/1-/'\(*'1|:\)'.=0,+\   monadic train; x: integer list
                    0,+\   prepend 0 to the cumulative sum of x
                   =       group; gives a dict of {value:indices}
                  .        extract values of the dict
                      any ordered pair inside each row forms a valid
                      (start,end) pair with subarray sum of zero
         (*'1|:\)'    for each list, extract (first,last)
 <!/1-/'\             sort this list by ascending order of (first-last)
*                     extract the first pair

It is actually shorter than a function that generates all contiguous subsequences and operates on them (which is \$\mathcal{O}(n^3)\$ time and space):

K (ngn/k), 26 bytes

{*(0=+/)#,/x{y'x}/:|!1+#x}

Try it online!

added 65 characters in body
Source Link
Bubbler
  • 79.3k
  • 5
  • 162
  • 484

K (ngn/k), 24 2222 21 bytes

*<!/1{t@'*<-/'\+&=/t:&x=/2#,:x}0,+\

Try it online!Try it online!

-1 byte thanks to Traws

Unfortunately(?), I found a 2-byte golf that bumps the time complexity to \$\mathcal{O}(n^2 \log n)\$ (and space complexity to \$\mathcal{O}(n^2)\$). This one utilizes "equality table" =/: and "deep where" & to extract all pairs of indices that represent zero-sum subarrays. Finding the pair that represents the longest such subarray is algorithmically the same.


K (ngn/k), 24 bytes

*<!/1-/'\(*'1|:\)'.=0,+\

Try it online!

Runs in \$\mathcal{O}(n \log n)\$ time and \$\mathcal{O}(n)\$ space. A monadic function train that takes a list of integers and returns [start index (inclusive), end index (exclusive)]. (For reading test case output, !0 and () are notations for empty lists, and ,0 is a notation for the singleton list [0].)

*<!/1-/'\(*'1|:\)'.=0,+\   monadic train; x: integer list
                    0,+\   prepend 0 to the cumulative sum of x
                   =       group; gives a dict of {value:indices}
                  .        extract values of the dict
                      any ordered pair inside each row forms a valid
                      (start,end) pair with subarray sum of zero
         (*'1|:\)'    for each list, extract (first,last)
 <!/1-/'\             sort this list by ascending order of (first-last)
*                     extract the first pair

It is actually shorter than a function that generates all contiguous subsequences and operates on them (which is \$\mathcal{O}(n^3)\$ time and space):

K (ngn/k), 26 bytes

{*(0=+/)#,/x{y'x}/:|!1+#x}

Try it online!

K (ngn/k), 24 22 bytes

*<!/1-/'\+&=/:/2#,0,+\

Try it online!

Unfortunately(?), I found a 2-byte golf that bumps the time complexity to \$\mathcal{O}(n^2 \log n)\$ (and space complexity to \$\mathcal{O}(n^2)\$). This one utilizes "equality table" =/: and "deep where" & to extract all pairs of indices that represent zero-sum subarrays. Finding the pair that represents the longest such subarray is the same.


K (ngn/k), 24 bytes

*<!/1-/'\(*'1|:\)'.=0,+\

Try it online!

Runs in \$\mathcal{O}(n \log n)\$ time and \$\mathcal{O}(n)\$ space. A monadic function train that takes a list of integers and returns [start index (inclusive), end index (exclusive)]. (For reading test case output, !0 and () are notations for empty lists, and ,0 is a notation for the singleton list [0].)

*<!/1-/'\(*'1|:\)'.=0,+\   monadic train; x: integer list
                    0,+\   prepend 0 to the cumulative sum of x
                   =       group; gives a dict of {value:indices}
                  .        extract values of the dict
                      any ordered pair inside each row forms a valid
                      (start,end) pair with subarray sum of zero
         (*'1|:\)'    for each list, extract (first,last)
 <!/1-/'\             sort this list by ascending order of (first-last)
*                     extract the first pair

It is actually shorter than a function that generates all contiguous subsequences and operates on them (which is \$\mathcal{O}(n^3)\$ time and space):

K (ngn/k), 26 bytes

{*(0=+/)#,/x{y'x}/:|!1+#x}

Try it online!

K (ngn/k), 24 22 21 bytes

{t@'*<-/t:&x=/:x}0,+\

Try it online!

-1 byte thanks to Traws

Unfortunately(?), I found a 2-byte golf that bumps the time complexity to \$\mathcal{O}(n^2 \log n)\$ (and space complexity to \$\mathcal{O}(n^2)\$). This one utilizes "equality table" =/: and "deep where" & to extract all pairs of indices that represent zero-sum subarrays. Finding the pair that represents the longest such subarray is algorithmically the same.


K (ngn/k), 24 bytes

*<!/1-/'\(*'1|:\)'.=0,+\

Try it online!

Runs in \$\mathcal{O}(n \log n)\$ time and \$\mathcal{O}(n)\$ space. A monadic function train that takes a list of integers and returns [start index (inclusive), end index (exclusive)]. (For reading test case output, !0 and () are notations for empty lists, and ,0 is a notation for the singleton list [0].)

*<!/1-/'\(*'1|:\)'.=0,+\   monadic train; x: integer list
                    0,+\   prepend 0 to the cumulative sum of x
                   =       group; gives a dict of {value:indices}
                  .        extract values of the dict
                      any ordered pair inside each row forms a valid
                      (start,end) pair with subarray sum of zero
         (*'1|:\)'    for each list, extract (first,last)
 <!/1-/'\             sort this list by ascending order of (first-last)
*                     extract the first pair

It is actually shorter than a function that generates all contiguous subsequences and operates on them (which is \$\mathcal{O}(n^3)\$ time and space):

K (ngn/k), 26 bytes

{*(0=+/)#,/x{y'x}/:|!1+#x}

Try it online!

added 1006 characters in body
Source Link
Bubbler
  • 79.3k
  • 5
  • 162
  • 484

K (ngn/k), 24 22 bytes

*<!/1-/'\+&=/:/2#,0,+\

Try it online!

Unfortunately(?), I found a 2-byte golf that bumps the time complexity to \$\mathcal{O}(n^2 \log n)\$ (and space complexity to \$\mathcal{O}(n^2)\$). This one utilizes "equality table" =/: and "deep where" & to extract all pairs of indices that represent zero-sum subarrays. Finding the pair that represents the longest such subarray is the same.


K (ngn/k), 24 bytes

*<!/1-/'\(*'1|:\)'.=0,+\

Try it online!

Runs in \$\mathcal{O}(n \log n)\$ time and \$\mathcal{O}(n)\$ space. A monadic function train that takes a list of integers and returns [start index (inclusive), end index (exclusive)]. (For reading test case output, !0 and () are notations for empty lists, and ,0 is a notation for the singleton list [0].)

*<!/1-/'\(*'1|:\)'.=0,+\   monadic train; x: integer list
                    0,+\   prepend 0 to the cumulative sum of x
                   =       group; gives a dict of {value:indices}
                  .        extract values of the dict
                      any ordered pair inside each row forms a valid
                      (start,end) pair with subarray sum of zero
         (*'1|:\)'    for each list, extract (first,last)
 <!/1-/'\             sort this list by ascending order of (first-last)
*                     extract the first pair

It is actually shorter than a function that generates all contiguous subsequences and operates on them (which is \$\mathcal{O}(n^3)\$ time and space):

K (ngn/k), 26 bytes

{*(0=+/)#,/x{y'x}/:|!1+#x}

Try it online!

K (ngn/k), 24 bytes

*<!/1-/'\(*'1|:\)'.=0,+\

Try it online!

Runs in \$\mathcal{O}(n \log n)\$ time and \$\mathcal{O}(n)\$ space. A monadic function train that takes a list of integers and returns [start index (inclusive), end index (exclusive)]. (For reading test case output, !0 and () are notations for empty lists, and ,0 is a notation for the singleton list [0].)

*<!/1-/'\(*'1|:\)'.=0,+\   monadic train; x: integer list
                    0,+\   prepend 0 to the cumulative sum of x
                   =       group; gives a dict of {value:indices}
                  .        extract values of the dict
                      any ordered pair inside each row forms a valid
                      (start,end) pair with subarray sum of zero
         (*'1|:\)'    for each list, extract (first,last)
 <!/1-/'\             sort this list by ascending order of (first-last)
*                     extract the first pair

It is actually shorter than a function that generates all contiguous subsequences and operates on them (which is \$\mathcal{O}(n^3)\$ time and space):

K (ngn/k), 26 bytes

{*(0=+/)#,/x{y'x}/:|!1+#x}

Try it online!

K (ngn/k), 24 22 bytes

*<!/1-/'\+&=/:/2#,0,+\

Try it online!

Unfortunately(?), I found a 2-byte golf that bumps the time complexity to \$\mathcal{O}(n^2 \log n)\$ (and space complexity to \$\mathcal{O}(n^2)\$). This one utilizes "equality table" =/: and "deep where" & to extract all pairs of indices that represent zero-sum subarrays. Finding the pair that represents the longest such subarray is the same.


K (ngn/k), 24 bytes

*<!/1-/'\(*'1|:\)'.=0,+\

Try it online!

Runs in \$\mathcal{O}(n \log n)\$ time and \$\mathcal{O}(n)\$ space. A monadic function train that takes a list of integers and returns [start index (inclusive), end index (exclusive)]. (For reading test case output, !0 and () are notations for empty lists, and ,0 is a notation for the singleton list [0].)

*<!/1-/'\(*'1|:\)'.=0,+\   monadic train; x: integer list
                    0,+\   prepend 0 to the cumulative sum of x
                   =       group; gives a dict of {value:indices}
                  .        extract values of the dict
                      any ordered pair inside each row forms a valid
                      (start,end) pair with subarray sum of zero
         (*'1|:\)'    for each list, extract (first,last)
 <!/1-/'\             sort this list by ascending order of (first-last)
*                     extract the first pair

It is actually shorter than a function that generates all contiguous subsequences and operates on them (which is \$\mathcal{O}(n^3)\$ time and space):

K (ngn/k), 26 bytes

{*(0=+/)#,/x{y'x}/:|!1+#x}

Try it online!

Source Link
Bubbler
  • 79.3k
  • 5
  • 162
  • 484
Loading