You are given an array A consisting of the integers −1, 0 and 1. A slice of that array is any pair of integers (P, Q) such that 0 ≤ P ≤ Q < N. Your task is to find the longest slice of A whose elements yield a non-negative sum.
Write a function:
class Solution { public int solution(int[] A); }
that, given an array A of length N, consisting only of the values −1, 0, 1, returns the length of the longest slice of A that yields a non-negative sum. If there's no such slice, your function should return 0.
For example, given A = [−1, −1, 1, −1, 1, 0, 1, −1, −1], your function should return 7, as the slice starting at the second position and ending at the eighth is the longest slice with a non-negative sum.
For another example, given A = [1, 1, −1, −1, −1, −1, −1, 1, 1] your function should return 4: both the first four elements and the last four elements of array A are longest valid slices.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [2..100,000];
- each element of array A is an integer within the range [−1..1].
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
int maxLengthTillNow = 0;
int currentMaxStart = -1;
int currentMaxEnd = -1;
for (int i = A.Length; i >= 1; i--)
{
if (maxLengthTillNow < (A.Length - i + 1))
{
for(int k = 0; k< (A.Length - i + 1);k++)
{
int currentSum = 0;
for (int j = k; j <= i+k-1; j++)
{
currentSum += A[j];
if (currentSum > 0 && (j-k+1) > maxLengthTillNow)
{
maxLengthTillNow = (j - k + 1);
currentMaxStart = k;
currentMaxEnd = j;
}
}
}
}
}
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
int maxLengthTillNow = 0;
int currentMaxStart = -1;
int currentMaxEnd = -1;
for (int i = A.Length; i >= 1; i--)
{
if (maxLengthTillNow < (A.Length - i + 1))
{
for(int k = 0; k< (A.Length - i + 1);k++)
{
int currentSum = 0;
for (int j = k; j <= i+k-1; j++)
{
currentSum += A[j];
if (currentSum > 0 && (j-k+1) > maxLengthTillNow)
{
maxLengthTillNow = (j - k + 1);
currentMaxStart = k;
currentMaxEnd = j;
}
}
}
}
}
return
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
int maxLengthTillNow = 0;
int currentMaxStart = -1;
int currentMaxEnd = -1;
for (int i = A.Length; i >= 1; i--)
{
if (maxLengthTillNow < (A.Length - i + 1))
{
for(int k = 0; k< (A.Length - i + 1);k++)
{
int currentSum = 0;
for (int j = k; j <= i+k-1; j++)
{
currentSum += A[j];
if (currentSum > 0 && (j-k+1) > maxLengthTillNow)
{
maxLengthTillNow = (j - k + 1);
currentMaxStart = k;
currentMaxEnd = j;
}
}
}
}
}
return maxLengthTillNow;
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
int maxLengthTillNow = 0;
int currentMaxStart = -1;
int currentMaxEnd = -1;
for (int i = A.Length; i >= 1; i--)
{
if (maxLengthTillNow < (A.Length - i + 1))
{
for(int k = 0; k< (A.Length - i + 1);k++)
{
int currentSum = 0;
for (int j = k; j <= i+k-1; j++)
{
currentSum += A[j];
if (currentSum > 0 && (j-k+1) > maxLengthTillNow)
{
maxLengthTillNow = (j - k + 1);
currentMaxStart = k;
currentMaxEnd = j;
}
}
}
}
}
return maxLengthTillNow;
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
int maxLengthTillNow = 0;
int currentMaxStart = -1;
int currentMaxEnd = -1;
for (int i = A.Length; i >= 1; i--)
{
if (maxLengthTillNow < (i))
{
for(int k = 0; k< (A.Length - i + 1);k++)
{
int currentSum = 0;
for (int j = k; j <= i+k-1; j++)
{
currentSum += A[j];
if (currentSum >= 0 && (j-k+1) > maxLengthTillNow)
{
maxLengthTillNow = (j - k + 1);
currentMaxStart = k;
currentMaxEnd = j;
}
}
}
}
}
return maxLengthTillNow;
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
int maxLengthTillNow = 0;
int currentMaxStart = -1;
int currentMaxEnd = -1;
for (int i = A.Length; i >= 1; i--)
{
if (maxLengthTillNow < (i))
{
for(int k = 0; k< (A.Length - i + 1);k++)
{
int currentSum = 0;
for (int j = k; j <= i+k-1; j++)
{
currentSum += A[j];
if (currentSum >= 0 && (j-k+1) > maxLengthTillNow)
{
maxLengthTillNow = (j - k + 1);
currentMaxStart = k;
currentMaxEnd = j;
}
}
}
}
}
return maxLengthTillNow;
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
int maxLengthTillNow = 0;
int currentMaxStart = -1;
int currentMaxEnd = -1;
for (int i = A.Length; i >= 1; i--)
{
if (maxLengthTillNow < (i))
{
for(int k = 0; k< (A.Length - i + 1);k++)
{
int currentSum = 0;
for (int j = k; j <= i+k-1; j++)
{
currentSum += A[j];
if (currentSum >= 0 && (j-k+1) > maxLengthTillNow)
{
maxLengthTillNow = (j - k + 1);
currentMaxStart = k;
currentMaxEnd = j;
}
}
}
}
}
return maxLengthTillNow;
}
}
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[] A) {
int maxLengthTillNow = 0;
int currentMaxStart = -1;
int currentMaxEnd = -1;
for (int i = A.Length; i >= 1; i--)
{
if (maxLengthTillNow < (i))
{
for(int k = 0; k< (A.Length - i + 1);k++)
{
int currentSum = 0;
for (int j = k; j <= i+k-1; j++)
{
currentSum += A[j];
if (currentSum >= 0 && (j-k+1) > maxLengthTillNow)
{
maxLengthTillNow = (j - k + 1);
currentMaxStart = k;
currentMaxEnd = j;
}
}
}
}
}
return maxLengthTillNow;
}
}
The following issues have been detected: timeout errors.
array of fibonacci words N = 100000
running time: >6.00 sec., time limit: 0.22 sec.