A non-empty array A consisting of N integers is given. A pair of integers (P, Q), such that 0 ≤ P < Q < N, is called a slice of array A (notice that the slice contains at least two elements). The average of a slice (P, Q) is the sum of A[P] + A[P + 1] + ... + A[Q] divided by the length of the slice. To be precise, the average equals (A[P] + A[P + 1] + ... + A[Q]) / (Q − P + 1).
For example, array A such that:
A[0] = 4 A[1] = 2 A[2] = 2 A[3] = 5 A[4] = 1 A[5] = 5 A[6] = 8contains the following example slices:
- slice (1, 2), whose average is (2 + 2) / 2 = 2;
- slice (3, 4), whose average is (5 + 1) / 2 = 3;
- slice (1, 4), whose average is (2 + 2 + 5 + 1) / 4 = 2.5.
The goal is to find the starting position of a slice whose average is minimal.
Write a function:
function solution(A);
that, given a non-empty array A consisting of N integers, returns the starting position of the slice with the minimal average. If there is more than one slice with a minimal average, you should return the smallest starting position of such a slice.
For example, given array A such that:
A[0] = 4 A[1] = 2 A[2] = 2 A[3] = 5 A[4] = 1 A[5] = 5 A[6] = 8the function should return 1, as explained above.
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 [−10,000..10,000].
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
}
}// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
// Calculate presum for easier averaging later. But since we need to use A values later, we need to create a new prefix sum array instead of reusing A.
let presum = new Array(A.length);
presum[0] = A[0];
for(let i = 1; i < A.length; ++i) {
presum[i] = presum[i - 1] + A[i];
}
// This is P, required since we need to know start index to calculate average, unlike in max sum slices problem, we just need to keep track of running sum there. But not here, we need to recalculate each time.
let startSliceIndex = 0;
// Value of the average value of slice so far.
let prevMinAverageSlice = Number.MAX_SAFE_INTEGER;
// Lowest P detected so far, defaults to 0.
let minSliceLowestIndexP = 0;
// Using Kadane's algorithm (?) to figure out the min average value (i.e. instead of using it to find max sum slice, we find min sum + average).
// Start from index 1 since no point to start from 0.
for(let q = 1; q < A.length; ++q) {
// Let's calculate average. After calculating the difference, we need to add back P, since we are taking difference (still need to include the value of the leftmost value which is P) - and difference is the sum of (P+1)..+.. (Q), hence need to add value of (P) itself. Same for divisor, need to add 1.
let averageWithQ = (presum[q] - presum[startSliceIndex] + A[startSliceIndex]) / (q - startSliceIndex + 1);
// Check current average with previous average to see which average is smaller.
if (averageWithQ < prevMinAverageSlice) {
prevMinAverageSlice = averageWithQ;
// We also need to
minSliceLowestIndexP = startSliceIndex;
}
// If A[q] itself is smaller than the new global average `prevMinAverageSlice`, it means A[q] helped to pull down the global average, and should discard the previous slices so far, and A[q] will be start of new slice.
if(A[q] < prevMinAverageSlice) {
startSliceIndex = q;
}
// if(averageWithQ < A[q]) {
// prevMinAverageSlice = av
// } else {
// P = q;
// }
// // If we found this element with previous average even smaller, then the slice will include this element. P no need to move in this case.
// if(averageWithQ < minAverageSliceSoFar) {
// minAverageSliceSoFar = averageWithQ;
// } else { // if not, then we need to see if just this element itself is it smaller than the average.
// if(A[q] < minAverageSliceSoFar) {
// minAverageSliceSoFar = A[q];
// P = q;
// }
// }
// minAverageSliceSoFar = Math.min(minAverageSliceSoFar, averageWithQ);
// At this point in time, would it be better to pick this element and previous element (to start a new slice), or to add on to the previous best lowest average slice so far.
}
return minSliceLowestIndexP;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
// Calculate presum for easier averaging later. But since we need to use A values later, we need to create a new prefix sum array instead of reusing A.
let presum = new Array(A.length);
presum[0] = A[0];
for(let i = 1; i < A.length; ++i) {
presum[i] = presum[i - 1] + A[i];
}
// This is P, required since we need to know start index to calculate average, unlike in max sum slices problem, we just need to keep track of running sum there. But not here, we need to recalculate each time.
let startSliceIndex = 0;
// Value of the average value of slice so far.
let prevMinAverageSlice = Number.MAX_SAFE_INTEGER;
// Lowest P detected so far, defaults to 0.
let minSliceLowestIndexP = 0;
// Using Kadane's algorithm (?) to figure out the min average value (i.e. instead of using it to find max sum slice, we find min sum + average).
// Start from index 1 since no point to start from 0.
for(let q = 1; q < A.length; ++q) {
// Let's calculate average. After calculating the difference, we need to add back P, since we are taking difference (still need to include the value of the leftmost value which is P) - and difference is the sum of (P+1)..+.. (Q), hence need to add value of (P) itself. Same for divisor, need to add 1.
let averageWithQ = (presum[q] - presum[startSliceIndex] + A[startSliceIndex]) / (q - startSliceIndex + 1);
// Check current average with previous average to see which average is smaller.
if (averageWithQ < prevMinAverageSlice) {
prevMinAverageSlice = averageWithQ;
// We also need to
minSliceLowestIndexP = startSliceIndex;
}
// If A[q] itself is smaller than the new global average `prevMinAverageSlice`, it means A[q] helped to pull down the global average, and should discard the previous slices so far, and A[q] will be start of new slice.
if(A[q] < prevMinAverageSlice) {
startSliceIndex = q;
}
// if(averageWithQ < A[q]) {
// prevMinAverageSlice = av
// } else {
// P = q;
// }
// // If we found this element with previous average even smaller, then the slice will include this element. P no need to move in this case.
// if(averageWithQ < minAverageSliceSoFar) {
// minAverageSliceSoFar = averageWithQ;
// } else { // if not, then we need to see if just this element itself is it smaller than the average.
// if(A[q] < minAverageSliceSoFar) {
// minAverageSliceSoFar = A[q];
// P = q;
// }
// }
// minAverageSliceSoFar = Math.min(minAverageSliceSoFar, averageWithQ);
// At this point in time, would it be better to pick this element and previous element (to start a new slice), or to add on to the previous best lowest average slice so far.
}
return minSliceLowestIndexP;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
// Calculate presum for easier averaging later. But since we need to use A values later, we need to create a new prefix sum array instead of reusing A.
let presum = new Array(A.length);
presum[0] = A[0];
for(let i = 1; i < A.length; ++i) {
presum[i] = presum[i - 1] + A[i];
}
// This is P, required since we need to know start index to calculate average, unlike in max sum slices problem, we just need to keep track of running sum there. But not here, we need to recalculate each time.
let startSliceIndex = 0;
// Value of the average value of slice so far.
let prevMinAverageSlice = Number.MAX_SAFE_INTEGER;
// Lowest P detected so far, defaults to 0.
let minSliceLowestIndexP = 0;
// Using Kadane's algorithm (?) to figure out the min average value (i.e. instead of using it to find max sum slice, we find min sum + average).
// Start from index 1 since no point to start from 0.
for(let q = 1; q < A.length; ++q) {
// Let's calculate average. After calculating the difference, we need to add back P, since we are taking difference (still need to include the value of the leftmost value which is P) - and difference is the sum of (P+1)..+.. (Q), hence need to add value of (P) itself. Same for divisor, need to add 1.
let averageWithQ = (presum[q] - presum[startSliceIndex] + A[startSliceIndex]) / (q - startSliceIndex + 1);
// Check current average with previous average to see which average is smaller.
if (averageWithQ < prevMinAverageSlice) {
prevMinAverageSlice = averageWithQ;
// We also need to
minSliceLowestIndexP = startSliceIndex;
}
// If A[q] itself is smaller than the new global average `prevMinAverageSlice`, it means A[q] helped to pull down the global average, and should discard the previous slices so far, and A[q] will be start of new slice.
if(A[q] < prevMinAverageSlice) {
startSliceIndex = q;
}
}
return minSliceLowestIndexP;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
// Calculate presum for easier averaging later. But since we need to use A values later, we need to create a new prefix sum array instead of reusing A.
let presum = new Array(A.length);
presum[0] = A[0];
for(let i = 1; i < A.length; ++i) {
presum[i] = presum[i - 1] + A[i];
}
// This is P, required since we need to know start index to calculate average, unlike in max sum slices problem, we just need to keep track of running sum there. But not here, we need to recalculate each time.
let startSliceIndex = 0;
// Value of the average value of slice so far. Set to ma
let prevMinAverageSlice = Number.MAX_SAFE_INTEGER;
// Lowest P detected so far, defaults to 0.
let minSliceLowestIndexP = 0;
// Using Kadane's algorithm (?) to figure out the min average value (i.e. instead of using it to find max sum slice, we find min sum + average).
// Start from index 1 since no point to start from 0.
for(let q = 1; q < A.length; ++q) {
// Let's calculate average. After calculating the difference, we need to add back P, since we are taking difference (still need to include the value of the leftmost value which is P) - and difference is the sum of (P+1)..+.. (Q), hence need to add value of (P) itself. Same for divisor, need to add 1.
let averageWithQ = (presum[q] - presum[startSliceIndex] + A[startSliceIndex]) / (q - startSliceIndex + 1);
// Check current average with previous average to see which average is smaller.
if (averageWithQ < prevMinAverageSlice) {
prevMinAverageSlice = averageWithQ;
// We also need to
minSliceLowestIndexP = startSliceIndex;
}
// If A[q] itself is smaller than the new global average `prevMinAverageSlice`, it means A[q] helped to pull down the global average, and should discard the previous slices so far, and A[q] will be start of new slice.
if(A[q] < prevMinAverageSlice) {
startSliceIndex = q;
}
}
return minSliceLowestIndexP;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
// Calculate presum for easier averaging later. But since we need to use A values later, we need to create a new prefix sum array instead of reusing A.
let presum = new Array(A.length);
presum[0] = A[0];
for(let i = 1; i < A.length; ++i) {
presum[i] = presum[i - 1] + A[i];
}
// This is P, required since we need to know start index to calculate average, unlike in max sum slices problem, we just need to keep track of running sum there. But not here, we need to recalculate each time.
let startSliceIndex = 0;
// Value of the average value of slice so far. Set to max since we are counting down.
let prevMinAverageSlice = Number.MAX_SAFE_INTEGER;
// Lowest P detected so far, defaults to 0.
let minSliceLowestIndexP = 0;
// Using Kadane's algorithm (?) to figure out the min average value (i.e. instead of using it to find max sum slice, we find min sum + average).
// Start from index 1 since no point to start from 0.
for(let q = 1; q < A.length; ++q) {
// Let's calculate average. After calculating the difference, we need to add back P, since we are taking difference (still need to include the value of the leftmost value which is P) - and difference is the sum of (P+1)..+.. (Q), hence need to add value of (P) itself. Same for divisor, need to add 1.
let averageWithQ = (presum[q] - presum[startSliceIndex] + A[startSliceIndex]) / (q - startSliceIndex + 1);
// Check current average with previous average to see which average is smaller.
if (averageWithQ < prevMinAverageSlice) {
prevMinAverageSlice = averageWithQ;
// We also need to
minSliceLowestIndexP = startSliceIndex;
}
// If A[q] itself is smaller than the new global average `prevMinAverageSlice`, it means A[q] helped to pull down the global average, and should discard the previous slices so far, and A[q] will be start of new slice.
if(A[q] < prevMinAverageSlice) {
startSliceIndex = q;
}
}
return minSliceLowestIndexP;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
// Calculate presum for easier averaging later. But since we need to use A values later, we need to create a new prefix sum array instead of reusing A.
let presum = new Array(A.length);
presum[0] = A[0];
for(let i = 1; i < A.length; ++i) {
presum[i] = presum[i - 1] + A[i];
}
// This is the working P, required since we need to know start index to calculate average, unlike in max sum slices problem, we just need to keep track of running sum there. But not here, we need to recalculate each time.
let startSliceIndex = 0;
// Value of the average value of slice so far. Set to max since we are counting down.
let prevMinAverageSlice = Number.MAX_SAFE_INTEGER;
// Lowest P detected so far, defaults to 0.
let minSliceLowestIndexP = 0;
// Using Kadane's algorithm (?) to figure out the min average value (i.e. instead of using it to find max sum slice, we find min sum + average).
// Start from index 1 since no point to start from 0.
for(let q = 1; q < A.length; ++q) {
// Let's calculate average. After calculating the difference, we need to add back P, since we are taking difference (still need to include the value of the leftmost value which is P) - and difference is the sum of (P+1)..+.. (Q), hence need to add value of (P) itself. Same for divisor, need to add 1.
let averageWithQ = (presum[q] - presum[startSliceIndex] + A[startSliceIndex]) / (q - startSliceIndex + 1);
// Check current average with previous average to see which average is smaller.
if (averageWithQ < prevMinAverageSlice) {
prevMinAverageSlice = averageWithQ;
// We also need to
minSliceLowestIndexP = startSliceIndex;
}
// If A[q] itself is smaller than the new global average `prevMinAverageSlice`, it means A[q] helped to pull down the global average, and should discard the previous slices so far, and A[q] will be start of new slice.
if(A[q] < prevMinAverageSlice) {
startSliceIndex = q;
}
}
return minSliceLowestIndexP;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
// Calculate presum for easier averaging later. But since we need to use A values later, we need to create a new prefix sum array instead of reusing A.
let presum = new Array(A.length);
presum[0] = A[0];
for(let i = 1; i < A.length; ++i) {
presum[i] = presum[i - 1] + A[i];
}
// This is the working P, required since we need to know start index to calculate average, unlike in max sum slices problem, we just need to keep track of running sum there. But not here, we need to recalculate each time.
let startSliceIndex = 0;
// Value of the average value of slice so far. Set to max since we are counting down.
let prevMinAverageSlice = Number.MAX_SAFE_INTEGER;
// Lowest P detected so far, defaults to 0.
let minSliceLowestIndexP = 0;
// Using Kadane's algorithm (?) to figure out the min average value (i.e. instead of using it to find max sum slice, we find min sum + average).
// Start from index 1 since no point to start from 0.
for(let q = 1; q < A.length; ++q) {
// Let's calculate average. After calculating the difference, we need to add back P, since we are taking difference (still need to include the value of the leftmost value which is P). and difference is the sum of (P+1)..+.. (Q), hence need to add value of (P) itself. Same for divisor, need to add 1.
let averageWithQ = (presum[q] - presum[startSliceIndex] + A[startSliceIndex]) / (q - startSliceIndex + 1);
// Check current average with previous average to see which average is smaller.
if (averageWithQ < prevMinAverageSlice) {
prevMinAverageSlice = averageWithQ;
// We also need to
minSliceLowestIndexP = startSliceIndex;
}
// If A[q] itself is smaller than the new global average `prevMinAverageSlice`, it means A[q] helped to pull down the global average, and should discard the previous slices so far, and A[q] will be start of new slice.
if(A[q] < prevMinAverageSlice) {
startSliceIndex = q;
}
}
return minSliceLowestIndexP;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
// Calculate presum for easier averaging later. But since we need to use A values later, we need to create a new prefix sum array instead of reusing A.
let presum = new Array(A.length);
presum[0] = A[0];
for(let i = 1; i < A.length; ++i) {
presum[i] = presum[i - 1] + A[i];
}
// This is the working P, required since we need to know start index to calculate average, unlike in max sum slices problem, we just need to keep track of running sum there. But not here, we need to recalculate each time.
let startSliceIndex = 0;
// Value of the average value of slice so far. Set to max since we are counting down.
let prevMinAverageSlice = Number.MAX_SAFE_INTEGER;
// Lowest P detected so far, defaults to 0.
let minSliceLowestIndexP = 0;
// Using Kadane's algorithm (?) to figure out the min average value (i.e. instead of using it to find max sum slice, we find min sum + average).
// Start from index 1 since no point to start from 0.
for(let q = 1; q < A.length; ++q) {
// Let's calculate average. After calculating the difference, we need to add back P, since we are taking difference (still need to include the value of the leftmost value which is P). And difference is the sum of (P+1)..+.. (Q), hence need to add value of (P) itself. Same for divisor, need to add 1.
let averageWithQ = (presum[q] - presum[startSliceIndex] + A[startSliceIndex]) / (q - startSliceIndex + 1);
// Check current average with previous average to see which average is smaller.
if (averageWithQ < prevMinAverageSlice) {
prevMinAverageSlice = averageWithQ;
// We also need to
minSliceLowestIndexP = startSliceIndex;
}
// If A[q] itself is smaller than the new global average `prevMinAverageSlice`, it means A[q] helped to pull down the global average, and should discard the previous slices so far, and A[q] will be start of new slice.
if(A[q] < prevMinAverageSlice) {
startSliceIndex = q;
}
}
return minSliceLowestIndexP;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
// Calculate presum for easier averaging later. But since we need to use A values later, we need to create a new prefix sum array instead of reusing A.
let presum = new Array(A.length);
presum[0] = A[0];
for(let i = 1; i < A.length; ++i) {
presum[i] = presum[i - 1] + A[i];
}
// This is the working P, required since we need to know start index to calculate average, unlike in max sum slices problem, we just need to keep track of running sum there. But not here, we need to recalculate each time.
let startSliceIndex = 0;
// Value of the average value of slice so far. Set to max since we are counting down.
let prevMinAverageSlice = Number.MAX_SAFE_INTEGER;
// Lowest P detected so far, defaults to 0.
let minSliceLowestIndexP = 0;
// Using Kadane's algorithm (?) to figure out the min average value (i.e. instead of using it to find max sum slice, we find min sum + average).
// Start from index 1 since no point to start from 0.
for(let q = 1; q < A.length; ++q) {
// Let's calculate average. After calculating the difference, we need to add back P, since we are taking difference (still need to include the value of the leftmost value which is P). Difference is the sum of (P+1)..+.. (Q), hence need to add value of (P) itself. Same for divisor, need to add 1.
let averageWithQ = (presum[q] - presum[startSliceIndex] + A[startSliceIndex]) / (q - startSliceIndex + 1);
// Check current average with previous average to see which average is smaller.
if (averageWithQ < prevMinAverageSlice) {
prevMinAverageSlice = averageWithQ;
// We also need to
minSliceLowestIndexP = startSliceIndex;
}
// If A[q] itself is smaller than the new global average `prevMinAverageSlice`, it means A[q] helped to pull down the global average, and should discard the previous slices so far, and A[q] will be start of new slice.
if(A[q] < prevMinAverageSlice) {
startSliceIndex = q;
}
}
return minSliceLowestIndexP;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
// Calculate presum for easier averaging later. But since we need to use A values later, we need to create a new prefix sum array instead of reusing A.
let presum = new Array(A.length);
presum[0] = A[0];
for(let i = 1; i < A.length; ++i) {
presum[i] = presum[i - 1] + A[i];
}
// This is the working P, required since we need to know start index to calculate average, unlike in max sum slices problem, we just need to keep track of running sum there. But not here, we need to recalculate each time.
let startSliceIndex = 0;
// Value of the average value of slice so far. Set to max since we are counting down.
let prevMinAverageSlice = Number.MAX_SAFE_INTEGER;
// Lowest P detected so far, defaults to 0.
let minSliceLowestIndexP = 0;
// Using Kadane's algorithm (?) to figure out the min average value (i.e. instead of using it to find max sum slice, we find min sum + average).
// Start from index 1 since no point to start from 0.
for(let q = 1; q < A.length; ++q) {
// Let's calculate average. After calculating the difference, we need to add back P, since we are taking difference (still need to include the value of the leftmost value which is P). Difference is the sum of (P+1)..+.. (Q), hence need to add value of (P) itself. Same for divisor, need to add 1.
let averageWithQ = (presum[q] - presum[startSliceIndex] + A[startSliceIndex]) / (q - startSliceIndex + 1);
// Check current average with previous average to see which average is smaller and we will update it to prep for next loop.
if (averageWithQ < prevMinAverageSlice) {
prevMinAverageSlice = averageWithQ;
// We also need to
minSliceLowestIndexP = startSliceIndex;
}
// If A[q] itself is smaller than the new global average `prevMinAverageSlice`, it means A[q] helped to pull down the global average, and should discard the previous slices so far, and A[q] will be start of new slice.
if(A[q] < prevMinAverageSlice) {
startSliceIndex = q;
}
}
return minSliceLowestIndexP;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
// Calculate presum for easier averaging later. But since we need to use A values later, we need to create a new prefix sum array instead of reusing A.
let presum = new Array(A.length);
presum[0] = A[0];
for(let i = 1; i < A.length; ++i) {
presum[i] = presum[i - 1] + A[i];
}
// This is the working P, required since we need to know start index to calculate average, unlike in max sum slices problem, we just need to keep track of running sum there. But not here, we need to recalculate each time.
let startSliceIndex = 0;
// Value of the average value of slice so far. Set to max since we are counting down.
let prevMinAverageSlice = Number.MAX_SAFE_INTEGER;
// Lowest P detected so far, defaults to 0.
let minSliceLowestIndexP = 0;
// Using Kadane's algorithm (?) to figure out the min average value (i.e. instead of using it to find max sum slice, we find min sum + average).
// Start from index 1 since no point to start from 0.
for(let q = 1; q < A.length; ++q) {
// Let's calculate average. After calculating the difference, we need to add back P, since we are taking difference (still need to include the value of the leftmost value which is P). Difference is the sum of (P+1)..+.. (Q), hence need to add value of (P) itself. Same for divisor, need to add 1.
let averageWithQ = (presum[q] - presum[startSliceIndex] + A[startSliceIndex]) / (q - startSliceIndex + 1);
// Check current average with previous average to see which average is smaller and we will update it to prep for next loop.
if (averageWithQ < prevMinAverageSlice) {
prevMinAverageSlice = averageWithQ;
// We also need to
minSliceLowestIndexP = startSliceIndex;
}
// If A[q] itself is smaller than the new global average `prevMinAverageSlice`, it means A[q] helped to pull down the global average, and should discard the previous slices so far, and A[q] will be start of new slice.
if(A[q] < prevMinAverageSlice) {
startSliceIndex = q;
}
}
return minSliceLowestIndexP;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
// Calculate presum for easier averaging later. But since we need to use A values later, we need to create a new prefix sum array instead of reusing A.
let presum = new Array(A.length);
presum[0] = A[0];
for(let i = 1; i < A.length; ++i) {
presum[i] = presum[i - 1] + A[i];
}
// This is the working P, required since we need to know start index to calculate average, unlike in max sum slices problem, we just need to keep track of running sum there. But not here, we need to recalculate each time.
let startSliceIndex = 0;
// Value of the average value of slice so far. Set to max since we are counting down.
let prevMinAverageSlice = Number.MAX_SAFE_INTEGER;
// Lowest P detected so far, defaults to 0.
let minSliceLowestIndexP = 0;
// Using Kadane's algorithm (?) to figure out the min average value (i.e. instead of using it to find max sum slice, we find min sum + average).
// Start from index 1 since no point to start from 0.
for(let q = 1; q < A.length; ++q) {
// Let's calculate average. After calculating the difference, we need to add back P, since we are taking difference (still need to include the value of the leftmost value which is P). Difference is the sum of (P+1)..+.. (Q), hence need to add value of (P) itself. Same for divisor, need to add 1.
let averageWithQ = (presum[q] - presum[startSliceIndex] + A[startSliceIndex]) / (q - startSliceIndex + 1);
// Check current average with previous average to see which average is smaller and we will update it to prep for next loop.
if (averageWithQ < prevMinAverageSlice) {
prevMinAverageSlice = averageWithQ;
// We also need to
minSliceLowestIndexP = startSliceIndex;
}
// If A[q] itself is smaller than the new global average `prevMinAverageSlice`, it means A[q] helped to pull down the global average, and should discard the previous slices so far, and A[q] will be start of new slice.
if(A[q] < prevMinAverageSlice) {
startSliceIndex = q;
// Note that startSliceIndex is not the CONFIRMED smallest slice yet, so we cannot use startSliceIndex as our return value.
}
}
return minSliceLowestIndexP;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
// Calculate presum for easier averaging later. But since we need to use A values later, we need to create a new prefix sum array instead of reusing A.
let presum = new Array(A.length);
presum[0] = A[0];
for(let i = 1; i < A.length; ++i) {
presum[i] = presum[i - 1] + A[i];
}
// This is the working P, required since we need to know start index to calculate average, unlike in max sum slices problem, we just need to keep track of running sum there. But not here, we need to recalculate each time.
let startSliceIndex = 0;
// Value of the average value of slice so far. Set to max since we are counting down.
let prevMinAverageSlice = Number.MAX_SAFE_INTEGER;
// Lowest P detected so far, defaults to 0.
let minSliceLowestIndexP = 0;
// Using Kadane's algorithm (?) to figure out the min average value (i.e. instead of using it to find max sum slice, we find min sum + average).
// Start from index 1 since no point to start from 0.
for(let q = 1; q < A.length; ++q) {
// Let's calculate average. After calculating the difference, we need to add back P, since we are taking difference (still need to include the value of the leftmost value which is P). Difference is the sum of (P+1)..+.. (Q), hence need to add value of (P) itself. Same for divisor, need to add 1.
let averageWithQ = (presum[q] - presum[startSliceIndex] + A[startSliceIndex]) / (q - startSliceIndex + 1);
// Check current average with previous average to see which average is smaller and we will update it to prep for next loop.
if (averageWithQ < prevMinAverageSlice) {
prevMinAverageSlice = averageWithQ;
// startSliceIndex is confirmed our smallest index fo
minSliceLowestIndexP = startSliceIndex;
}
// If A[q] itself is smaller than the new global average `prevMinAverageSlice`, it means A[q] helped to pull down the global average, and should discard the previous slices so far, and A[q] will be start of new slice.
if(A[q] < prevMinAverageSlice) {
startSliceIndex = q;
// Note that startSliceIndex is not the CONFIRMED smallest slice yet, so we cannot use startSliceIndex as our return value.
}
}
return minSliceLowestIndexP;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
// Calculate presum for easier averaging later. But since we need to use A values later, we need to create a new prefix sum array instead of reusing A.
let presum = new Array(A.length);
presum[0] = A[0];
for(let i = 1; i < A.length; ++i) {
presum[i] = presum[i - 1] + A[i];
}
// This is the working P, required since we need to know start index to calculate average, unlike in max sum slices problem, we just need to keep track of running sum there. But not here, we need to recalculate each time.
let startSliceIndex = 0;
// Value of the average value of slice so far. Set to max since we are counting down.
let prevMinAverageSlice = Number.MAX_SAFE_INTEGER;
// Lowest P detected so far, defaults to 0.
let minSliceLowestIndexP = 0;
// Using Kadane's algorithm (?) to figure out the min average value (i.e. instead of using it to find max sum slice, we find min sum + average).
// Start from index 1 since no point to start from 0.
for(let q = 1; q < A.length; ++q) {
// Let's calculate average. After calculating the difference, we need to add back P, since we are taking difference (still need to include the value of the leftmost value which is P). Difference is the sum of (P+1)..+.. (Q), hence need to add value of (P) itself. Same for divisor, need to add 1.
let averageWithQ = (presum[q] - presum[startSliceIndex] + A[startSliceIndex]) / (q - startSliceIndex + 1);
// Check current average with previous average to see which average is smaller and we will update it to prep for next loop.
if (averageWithQ < prevMinAverageSlice) {
prevMinAverageSlice = averageWithQ;
// startSliceIndex is confirmed our smallest start index for now.
minSliceLowestIndexP = startSliceIndex;
}
// If A[q] itself is smaller than the new global average `prevMinAverageSlice`, it means A[q] helped to pull down the global average, and should discard the previous slices so far, and A[q] will be start of new slice.
if(A[q] < prevMinAverageSlice) {
startSliceIndex = q;
// Note that startSliceIndex is not the CONFIRMED smallest slice yet, so we cannot use startSliceIndex as our return value.
}
}
return minSliceLowestIndexP;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
// Calculate presum for easier averaging later. But since we need to use A values later, we need to create a new prefix sum array instead of reusing A.
let presum = new Array(A.length);
presum[0] = A[0];
for(let i = 1; i < A.length; ++i) {
presum[i] = presum[i - 1] + A[i];
}
// This is the working P, required since we need to know start index to calculate average, unlike in max sum slices problem, we just need to keep track of running sum there. But not here, we need to recalculate each time.
let startSliceIndex = 0;
// Value of the average value of slice so far. Set to max since we are counting down.
let prevMinAverageSlice = Number.MAX_SAFE_INTEGER;
// Lowest P detected so far, defaults to 0.
let minSliceLowestIndexP = 0;
// Using Kadane's algorithm (?) to figure out the min average value (i.e. instead of using it to find max sum slice, we find min sum + average).
// Start from index 1 since no point to start from 0.
for(let q = 1; q < A.length; ++q) {
// Let's calculate average. After calculating the difference, we need to add back P, since we are taking difference (still need to include the value of the leftmost value which is P). Difference is the sum of (P+1)..+.. (Q), hence need to add value of (P) itself. Same for divisor, need to add 1.
let averageWithQ = (presum[q] - presum[startSliceIndex] + A[startSliceIndex]) / (q - startSliceIndex + 1);
// Check current average with previous average to see which average is smaller and we will update it to prep for next loop.
if (averageWithQ < prevMinAverageSlice) {
prevMinAverageSlice = averageWithQ;
// startSliceIndex is confirmed our smallest start index for now.
minSliceLowestIndexP = startSliceIndex;
}
// If A[q] itself is smaller than the new global average `prevMinAverageSlice`, it means A[q] helped to pull down the global average, and should discard the previous slices so far, and A[q] will be start of new slice.
if(A[q] < prevMinAverageSlice) {
startSliceIndex = q;
// Note that startSliceIndex is not the CONFIRMED smallest slice yet, so we cannot use startSliceIndex as our return value, and hence we have a minSliceLowestIndexP.
}
}
return minSliceLowestIndexP;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
// Calculate presum for easier averaging later. But since we need to use A values later, we need to create a new prefix sum array instead of reusing A.
let presum = new Array(A.length);
presum[0] = A[0];
for(let i = 1; i < A.length; ++i) {
presum[i] = presum[i - 1] + A[i];
}
// This is the working P, required since we need to know start index to calculate average, unlike in max sum slices problem, we just need to keep track of running sum there. But not here, we need to recalculate each time.
let startSliceIndex = 0;
// Value of the average value of slice so far. Set to max since we are counting down.
let prevMinAverageSlice = Number.MAX_SAFE_INTEGER;
// Lowest P detected so far, defaults to 0.
let minSliceLowestIndexP = 0;
// Using Kadane's algorithm (?) to figure out the min average value (i.e. instead of using it to find max sum slice, we find min sum + average).
// Start from index 1 since no point to start from 0.
for(let q = 1; q < A.length; ++q) {
// Let's calculate average. After calculating the difference, we need to add back P, since we are taking difference (still need to include the value of the leftmost value which is P). Difference is the sum of (P+1)..+.. (Q), hence need to add value of (P) itself. Same for divisor, need to add 1.
let averageWithQ = (presum[q] - presum[startSliceIndex] + A[startSliceIndex]) / (q - startSliceIndex + 1);
// Check current average with previous average to see which average is smaller and we will update it to prep for next loop.
if (averageWithQ < prevMinAverageSlice) {
prevMinAverageSlice = averageWithQ;
// startSliceIndex is confirmed our smallest start index for now, so we just store it everytime.
minSliceLowestIndexP = startSliceIndex;
}
// If A[q] itself is smaller than the new global average `prevMinAverageSlice`, it means A[q] helped to pull down the global average, and should discard the previous slices so far, and A[q] will be start of new slice.
if(A[q] < prevMinAverageSlice) {
startSliceIndex = q;
// Note that startSliceIndex is not the CONFIRMED smallest slice yet, so we cannot use startSliceIndex as our return value, and hence we have a minSliceLowestIndexP.
}
}
return minSliceLowestIndexP;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
// Calculate presum for easier averaging later. But since we need to use A values later, we need to create a new prefix sum array instead of reusing A.
let presum = new Array(A.length);
presum[0] = A[0];
for(let i = 1; i < A.length; ++i) {
presum[i] = presum[i - 1] + A[i];
}
// This is the working P, required since we need to know start index to calculate average, unlike in max sum slices problem, we just need to keep track of running sum there. But not here, we need to recalculate each time.
let startSliceIndex = 0;
// Value of the average value of slice so far. Set to max since we are counting down.
let prevMinAverageSlice = Number.MAX_SAFE_INTEGER;
// Lowest P detected so far, defaults to 0.
let minSliceLowestIndexP = 0;
// Using Kadane's algorithm (?) to figure out the min average value (i.e. instead of using it to find max sum slice, we find min sum + average).
// Start from index 1 since no point to start from 0.
for(let q = 1; q < A.length; ++q) {
// Let's calculate average. After calculating the difference, we need to add back P, since we are taking difference (still need to include the value of the leftmost value which is P). Difference is the sum of (P+1)..+.. (Q), hence need to add value of (P) itself. Same for divisor, need to add 1.
let averageWithQ = (presum[q] - presum[startSliceIndex] + A[startSliceIndex]) / (q - startSliceIndex + 1);
// Check current average with previous average to see which average is smaller and we will update it to prep for next loop.
if (averageWithQ < prevMinAverageSlice) {
prevMinAverageSlice = averageWithQ;
// startSliceIndex is confirmed our smallest start index for now, so we just store it everytime.
minSliceLowestIndexP = startSliceIndex;
}
// If A[q] itself is smaller than the new global average `prevMinAverageSlice`, it means A[q] helped to pull down the global average, and should discard the previous slices so far, and A[q] will be start of new slice.
if(A[q] < prevMinAverageSlice) {
startSliceIndex = q;
// Note that startSliceIndex is not the CONFIRMED smallest slice yet, so we cannot use startSliceIndex as our return value, and hence we have a minSliceLowestIndexP.
}
}
return minSliceLowestIndexP;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
// Calculate presum for easier averaging later. But since we need to use A values later, we need to create a new prefix sum array instead of reusing A.
let presum = new Array(A.length);
presum[0] = A[0];
for(let i = 1; i < A.length; ++i) {
presum[i] = presum[i - 1] + A[i];
}
// This is the working P, required since we need to know start index to calculate average, unlike in max sum slices problem, we just need to keep track of running sum there. But not here, we need to recalculate each time.
let startSliceIndex = 0;
// Value of the average value of slice so far. Set to max since we are counting down.
let prevMinAverageSlice = Number.MAX_SAFE_INTEGER;
// Lowest P detected so far, defaults to 0.
let minSliceLowestIndexP = 0;
// Using Kadane's algorithm (?) to figure out the min average value (i.e. instead of using it to find max sum slice, we find min sum + average).
// Start from index 1 since no point to start from 0.
for(let q = 1; q < A.length; ++q) {
// Let's calculate average. After calculating the difference, we need to add back P, since we are taking difference (still need to include the value of the leftmost value which is P). Difference is the sum of (P+1)..+.. (Q), hence need to add value of (P) itself. Same for divisor, need to add 1.
let averageWithQ = (presum[q] - presum[startSliceIndex] + A[startSliceIndex]) / (q - startSliceIndex + 1);
// Check current average with previous average to see which average is smaller and we will update it to prep for next loop.
if (averageWithQ < prevMinAverageSlice) {
prevMinAverageSlice = averageWithQ;
// startSliceIndex is confirmed our smallest start index for now, so we just store it everytime.
minSliceLowestIndexP = startSliceIndex;
}
// If A[q] itself is smaller than the new global average `prevMinAverageSlice`, it means A[q] helped to pull down the global average, and should discard the previous slices so far, and A[q] will be start of new slice.
if(A[q] < prevMinAverageSlice) {
startSliceIndex = q;
// Note that startSliceIndex is not the CONFIRMED smallest slice yet, so we cannot use startSliceIndex as our return value, and hence we have a minSliceLowestIndexP.
}
}
return minSliceLowestIndexP;
}
The solution obtained perfect score.