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:
class Solution { public int solution(int[] 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].
class Solution {
public int solution(int[] A) {
int minAvgIdx=0;
int minAvgVal=(A[0]+A[1])/2; //At least two elements in A.
double currAvg;
for(int i=0; i<A.length-2; i++){
/**
* We check first the two-element slice
*/
currAvg = (A[i] + A[i+1])/2;
if(currAvg < minAvgVal){
minAvgVal = currAvg;
minAvgIdx = i;
}
/**
* We check the three-element slice
*/
currAvg = (A[i] + A[i+1] + A[i+2])/3;
if(currAvg < minAvgVal){
minAvgVal = currAvg;
minAvgIdx = i;
}
}
/**
* Now we have to check the remaining two elements of the array
* Inside the for we checked ALL the three-element slices (the last one
* began at A.length-3) and all but one two-element slice (the missing
* one begins at A.length-2).
*/
currAvg = (A[length-2] + A[length-1])/2;
if(currAvg < minAvgVal){
minAvgVal = currAvg;
minAvgIdx = i;
}
return currAvg;
}
}
Solution.java:12: error: possible loss of precision minAvgVal = currAvg; ^ required: int found: double Solution.java:20: error: possible loss of precision minAvgVal = currAvg; ^ required: int found: double Solution.java:30: error: cannot find symbol currAvg = (A[length-2] + A[length-1])/2; ^ symbol: variable length location: class Solution Solution.java:30: error: cannot find symbol currAvg = (A[length-2] + A[length-1])/2; ^ symbol: variable length location: class Solution Solution.java:32: error: possible loss of precision minAvgVal = currAvg; ^ required: int found: double Solution.java:33: error: cannot find symbol minAvgIdx = i; ^ symbol: variable i location: class Solution Solution.java:35: error: possible loss of precision return currAvg; ^ required: int found: double 7 errors
class Solution {
public int solution(int[] A) {
int minAvgIdx=0;
double minAvgVal=(A[0]+A[1])/2; //At least two elements in A.
double currAvg;
for(int i=0; i<A.length-2; i++){
/**
* We check first the two-element slice
*/
currAvg = (double)((A[i] + A[i+1])/2);
if(currAvg < minAvgVal){
minAvgVal = currAvg;
minAvgIdx = i;
}
/**
* We check the three-element slice
*/
currAvg = (double)((A[i] + A[i+1] + A[i+2])/3);
if(currAvg < minAvgVal){
minAvgVal = currAvg;
minAvgIdx = i;
}
}
/**
* Now we have to check the remaining two elements of the array
* Inside the for we checked ALL the three-element slices (the last one
* began at A.length-3) and all but one two-element slice (the missing
* one begins at A.length-2).
*/
currAvg = (double)((A[length-2] + A[length-1])/2);
if(currAvg < minAvgVal){
minAvgVal = currAvg;
minAvgIdx = i;
}
return currAvg;
}
}
Solution.java:30: error: cannot find symbol currAvg = (double)((A[length-2] + A[length-1])/2); ^ symbol: variable length location: class Solution Solution.java:30: error: cannot find symbol currAvg = (double)((A[length-2] + A[length-1])/2); ^ symbol: variable length location: class Solution Solution.java:33: error: cannot find symbol minAvgIdx = i; ^ symbol: variable i location: class Solution Solution.java:35: error: possible loss of precision return currAvg; ^ required: int found: double 4 errors
class Solution {
public int solution(int[] A) {
int minAvgIdx=0;
double minAvgVal=(A[0]+A[1])/2; //At least two elements in A.
double currAvg;
for(int i=0; i<A.length-2; i++){
/**
* We check first the two-element slice
*/
currAvg = (double)((A[i] + A[i+1])/2);
if(currAvg < minAvgVal){
minAvgVal = currAvg;
minAvgIdx = i;
}
/**
* We check the three-element slice
*/
currAvg = (double)((A[i] + A[i+1] + A[i+2])/3);
if(currAvg < minAvgVal){
minAvgVal = currAvg;
minAvgIdx = i;
}
}
/**
* Now we have to check the remaining two elements of the array
* Inside the for we checked ALL the three-element slices (the last one
* began at A.length-3) and all but one two-element slice (the missing
* one begins at A.length-2).
*/
currAvg = (double)((A[A.length-2] + A[A.length-1])/2);
if(currAvg < minAvgVal){
minAvgVal = currAvg;
minAvgIdx = i;
}
return currAvg;
}
}
Solution.java:33: error: cannot find symbol minAvgIdx = i; ^ symbol: variable i location: class Solution Solution.java:35: error: possible loss of precision return currAvg; ^ required: int found: double 2 errors
class Solution {
public int solution(int[] A) {
int minAvgIdx=0;
double minAvgVal=(A[0]+A[1])/2; //At least two elements in A.
double currAvg;
for(int i=0; i<A.length-2; i++){
/**
* We check first the two-element slice
*/
currAvg = (double)((A[i] + A[i+1])/2);
if(currAvg < minAvgVal){
minAvgVal = currAvg;
minAvgIdx = i;
}
/**
* We check the three-element slice
*/
currAvg = (double)((A[i] + A[i+1] + A[i+2])/3);
if(currAvg < minAvgVal){
minAvgVal = currAvg;
minAvgIdx = i;
}
}
/**
* Now we have to check the remaining two elements of the array
* Inside the for we checked ALL the three-element slices (the last one
* began at A.length-3) and all but one two-element slice (the missing
* one begins at A.length-2).
*/
currAvg = (double)((A[A.length-2] + A[A.length-1])/2);
if(currAvg < minAvgVal){
minAvgVal = currAvg;
minAvgIdx = A.length-2;
}
return minAvgIdx;
}
}
class Solution {
public int solution(int[] A) {
int minAvgIdx=0;
double minAvgVal=(A[0]+A[1])/2; //At least two elements in A.
double currAvg;
for(int i=0; i<A.length-2; i++){
/**
* We check first the two-element slice
*/
currAvg = (double)((A[i] + A[i+1])/2);
System.out.println("currAvg for two-element slice is " + currAvg);
System.out.println("minAvgVal is " + minAvgVal);
if(currAvg < minAvgVal){
System.out.println("setting new min with two-element slice");
minAvgVal = currAvg;
minAvgIdx = i;
}
/**
* We check the three-element slice
*/
currAvg = (double)((A[i] + A[i+1] + A[i+2])/3);
System.out.println("currAvg for three-element slice is " currAvg);
System.out.println("minAvgVal is " + minAvgVal);
if(currAvg < minAvgVal){
System.out.println("setting new min with three-element slice");
minAvgVal = currAvg;
minAvgIdx = i;
}
}
/**
* Now we have to check the remaining two elements of the array
* Inside the for we checked ALL the three-element slices (the last one
* began at A.length-3) and all but one two-element slice (the missing
* one begins at A.length-2).
*/
currAvg = (double)((A[A.length-2] + A[A.length-1])/2);
if(currAvg < minAvgVal){
minAvgVal = currAvg;
minAvgIdx = A.length-2;
}
return minAvgIdx;
}
}
Solution.java:22: error: ')' expected System.out.println("currAvg for three-element slice is " currAvg); ^ Solution.java:22: error: illegal start of expression System.out.println("currAvg for three-element slice is " currAvg); ^ 2 errors
class Solution {
public int solution(int[] A) {
int minAvgIdx=0;
double minAvgVal=(A[0]+A[1])/2; //At least two elements in A.
double currAvg;
for(int i=0; i<A.length-2; i++){
/**
* We check first the two-element slice
*/
currAvg = (double)((A[i] + A[i+1])/2);
System.out.println("currAvg for two-element slice is " + currAvg);
System.out.println("minAvgVal is " + minAvgVal);
if(currAvg < minAvgVal){
System.out.println("setting new min with two-element slice");
minAvgVal = currAvg;
minAvgIdx = i;
}
/**
* We check the three-element slice
*/
currAvg = (double)((A[i] + A[i+1] + A[i+2])/3);
System.out.println("currAvg for three-element slice is " + currAvg);
System.out.println("minAvgVal is " + minAvgVal);
if(currAvg < minAvgVal){
System.out.println("setting new min with three-element slice");
minAvgVal = currAvg;
minAvgIdx = i;
}
}
/**
* Now we have to check the remaining two elements of the array
* Inside the for we checked ALL the three-element slices (the last one
* began at A.length-3) and all but one two-element slice (the missing
* one begins at A.length-2).
*/
currAvg = (double)((A[A.length-2] + A[A.length-1])/2);
if(currAvg < minAvgVal){
minAvgVal = currAvg;
minAvgIdx = A.length-2;
}
return minAvgIdx;
}
}
currAvg for two-element slice is 3.0 minAvgVal is 3.0 currAvg for three-element slice is 2.0 minAvgVal is 3.0 setting new min with three-element slice currAvg for two-element slice is 2.0 minAvgVal is 2.0 currAvg for three-element slice is 3.0 minAvgVal is 2.0 currAvg for two-element slice is 3.0 minAvgVal is 2.0 currAvg for three-element slice is 2.0 minAvgVal is 2.0 currAvg for two-element slice is 3.0 minAvgVal is 2.0 currAvg for three-element slice is 3.0 minAvgVal is 2.0 currAvg for two-element slice is 3.0 minAvgVal is 2.0 currAvg for three-element slice is 4.0 minAvgVal is 2.0
class Solution {
public int solution(int[] A) {
int minAvgIdx=0;
double minAvgVal=(A[0]+A[1])/2; //At least two elements in A.
double currAvg;
for(int i=0; i<A.length-2; i++){
/**
* We check first the two-element slice
*/
currAvg = ((double)(A[i] + A[i+1]))/2;
System.out.println("currAvg for two-element slice is " + currAvg);
System.out.println("minAvgVal is " + minAvgVal);
if(currAvg < minAvgVal){
System.out.println("setting new min with two-element slice");
minAvgVal = currAvg;
minAvgIdx = i;
}
/**
* We check the three-element slice
*/
currAvg = ((double)(A[i] + A[i+1] + A[i+2]))/3;
System.out.println("currAvg for three-element slice is " + currAvg);
System.out.println("minAvgVal is " + minAvgVal);
if(currAvg < minAvgVal){
System.out.println("setting new min with three-element slice");
minAvgVal = currAvg;
minAvgIdx = i;
}
}
/**
* Now we have to check the remaining two elements of the array
* Inside the for we checked ALL the three-element slices (the last one
* began at A.length-3) and all but one two-element slice (the missing
* one begins at A.length-2).
*/
currAvg = ((double)(A[A.length-2] + A[A.length-1]))/2;
if(currAvg < minAvgVal){
minAvgVal = currAvg;
minAvgIdx = A.length-2;
}
return minAvgIdx;
}
}
WARNING: producing output may seriously slow down your code! currAvg for two-element slice is 3.0 minAvgVal is 3.0 currAvg for three-element slice is 2.6666666666666665 minAvgVal is 3.0 setting new min with three-element slice currAvg for two-element slice is 2.0 minAvgVal is 2.6666666666666665 setting new min with two-element slice currAvg for three-element slice is 3.0 minAvgVal is 2.0 currAvg for two-element slice is 3.5 minAvgVal is 2.0 currAvg for three-element slice is 2.6666666666666665 minAvgVal is 2.0 currAvg for two-element slice is 3.0 minAvgVal is 2.0 currAvg for three-element slice is 3.6666666666666665 minAvgVal is 2.0 currAvg for two-element slice is 3.0 minAvgVal is 2.0 currAvg for three-element slice is 4.666666666666667 minAvgVal is 2.0
class Solution {
public int solution(int[] A) {
int minAvgIdx=0;
double minAvgVal=(A[0]+A[1])/2; //At least two elements in A.
double currAvg;
for(int i=0; i<A.length-2; i++){
/**
* We check first the two-element slice
*/
currAvg = ((double)(A[i] + A[i+1]))/2;
System.out.println("currAvg for two-element slice is " + currAvg);
System.out.println("minAvgVal is " + minAvgVal);
if(currAvg < minAvgVal){
System.out.println("setting new min with two-element slice");
minAvgVal = currAvg;
minAvgIdx = i;
}
/**
* We check the three-element slice
*/
currAvg = ((double)(A[i] + A[i+1] + A[i+2]))/3;
System.out.println("currAvg for three-element slice is " + currAvg);
System.out.println("minAvgVal is " + minAvgVal);
if(currAvg < minAvgVal){
System.out.println("setting new min with three-element slice");
minAvgVal = currAvg;
minAvgIdx = i;
}
}
/**
* Now we have to check the remaining two elements of the array
* Inside the for we checked ALL the three-element slices (the last one
* began at A.length-3) and all but one two-element slice (the missing
* one begins at A.length-2).
*/
currAvg = ((double)(A[A.length-2] + A[A.length-1]))/2;
if(currAvg < minAvgVal){
minAvgVal = currAvg;
minAvgIdx = A.length-2;
}
return minAvgIdx;
}
}
WARNING: producing output may seriously slow down your code! currAvg for two-element slice is 3.0 minAvgVal is 3.0 currAvg for three-element slice is 2.6666666666666665 minAvgVal is 3.0 setting new min with three-element slice currAvg for two-element slice is 2.0 minAvgVal is 2.6666666666666665 setting new min with two-element slice currAvg for three-element slice is 3.0 minAvgVal is 2.0 currAvg for two-element slice is 3.5 minAvgVal is 2.0 currAvg for three-element slice is 2.6666666666666665 minAvgVal is 2.0 currAvg for two-element slice is 3.0 minAvgVal is 2.0 currAvg for three-element slice is 3.6666666666666665 minAvgVal is 2.0 currAvg for two-element slice is 3.0 minAvgVal is 2.0 currAvg for three-element slice is 4.666666666666667 minAvgVal is 2.0
function result: 3
currAvg for two-element slice is 1.0 minAvgVal is 1.0 currAvg for three-element slice is 3.3333333333333335 minAvgVal is 1.0 currAvg for two-element slice is 4.5 minAvgVal is 1.0 currAvg for three-element slice is 3.3333333333333335 minAvgVal is 1.0 currAvg for two-element slice is 4.5 minAvgVal is 1.0 currAvg for three-element slice is 0.3333333333333333 minAvgVal is 1.0 setting new min with three-element slice currAvg for two-element slice is -3.5 minAvgVal is 0.3333333333333333 setting new min with two-element slice currAvg for three-element slice is -2.0 minAvgVal is -3.5
function result: 0
function result: 1
currAvg for two-element slice is 3.0 minAvgVal is 3.0 currAvg for three-element slice is 3.6666666666666665 minAvgVal is 3.0 currAvg for two-element slice is 3.5 minAvgVal is 3.0 currAvg for three-element slice is 2.6666666666666665 minAvgVal is 3.0 setting new min with three-element slice currAvg for two-element slice is 3.0 minAvgVal is 2.6666666666666665 currAvg for three-element slice is 3.6666666666666665 minAvgVal is 2.6666666666666665 currAvg for two-element slice is 3.0 minAvgVal is 2.6666666666666665 currAvg for three-element slice is 4.666666666666667 minAvgVal is 2.6666666666666665
class Solution {
public int solution(int[] A) {
int minAvgIdx=0;
double minAvgVal=(A[0]+A[1])/2; //At least two elements in A.
double currAvg;
for(int i=0; i<A.length-2; i++){
System.out.println("Startin with position " + i);
/**
* We check first the two-element slice
*/
currAvg = ((double)(A[i] + A[i+1]))/2;
System.out.println("currAvg for two-element slice is " + currAvg);
System.out.println("minAvgVal is " + minAvgVal);
if(currAvg < minAvgVal){
System.out.println("setting new min with two-element slice");
minAvgVal = currAvg;
minAvgIdx = i;
}
/**
* We check the three-element slice
*/
currAvg = ((double)(A[i] + A[i+1] + A[i+2]))/3;
System.out.println("currAvg for three-element slice is " + currAvg);
System.out.println("minAvgVal is " + minAvgVal);
if(currAvg < minAvgVal){
System.out.println("setting new min with three-element slice");
minAvgVal = currAvg;
minAvgIdx = i;
}
}
/**
* Now we have to check the remaining two elements of the array
* Inside the for we checked ALL the three-element slices (the last one
* began at A.length-3) and all but one two-element slice (the missing
* one begins at A.length-2).
*/
currAvg = ((double)(A[A.length-2] + A[A.length-1]))/2;
if(currAvg < minAvgVal){
minAvgVal = currAvg;
minAvgIdx = A.length-2;
}
return minAvgIdx;
}
}
WARNING: producing output may seriously slow down your code! Startin with position 0 currAvg for two-element slice is 3.0 minAvgVal is 3.0 currAvg for three-element slice is 2.6666666666666665 minAvgVal is 3.0 setting new min with three-element slice Startin with position 1 currAvg for two-element slice is 2.0 minAvgVal is 2.6666666666666665 setting new min with two-element slice currAvg for three-element slice is 3.0 minAvgVal is 2.0 Startin with position 2 currAvg for two-element slice is 3.5 minAvgVal is 2.0 currAvg for three-element slice is 2.6666666666666665 minAvgVal is 2.0 Startin with position 3 currAvg for two-element slice is 3.0 minAvgVal is 2.0 currAvg for three-element slice is 3.6666666666666665 minAvgVal is 2.0 Startin with position 4 currAvg for two-element slice is 3.0 minAvgVal is 2.0 currAvg for three-element slice is 4.666666666666667 minAvgVal is 2.0
function result: 3
Startin with position 0 currAvg for two-element slice is 1.0 minAvgVal is 1.0 currAvg for three-element slice is 3.3333333333333335 minAvgVal is 1.0 Startin with position 1 currAvg for two-element slice is 4.5 minAvgVal is 1.0 currAvg for three-element slice is 3.3333333333333335 minAvgVal is 1.0 Startin with position 2 currAvg for two-element slice is 4.5 minAvgVal is 1.0 currAvg for three-element slice is 0.3333333333333333 minAvgVal is 1.0 setting new min with three-element slice Startin with position 3 currAvg for two-element slice is -3.5 minAvgVal is 0.3333333333333333 setting new min with two-element slice currAvg for three-element slice is -2.0 minAvgVal is -3.5
function result: 0
function result: 1
Startin with position 0 currAvg for two-element slice is 3.0 minAvgVal is 3.0 currAvg for three-element slice is 3.6666666666666665 minAvgVal is 3.0 Startin with position 1 currAvg for two-element slice is 3.5 minAvgVal is 3.0 currAvg for three-element slice is 2.6666666666666665 minAvgVal is 3.0 setting new min with three-element slice Startin with position 2 currAvg for two-element slice is 3.0 minAvgVal is 2.6666666666666665 currAvg for three-element slice is 3.6666666666666665 minAvgVal is 2.6666666666666665 Startin with position 3 currAvg for two-element slice is 3.0 minAvgVal is 2.6666666666666665 currAvg for three-element slice is 4.666666666666667 minAvgVal is 2.6666666666666665
class Solution {
public int solution(int[] A) {
int minAvgIdx=0;
double minAvgVal=(A[0]+A[1])/2; //At least two elements in A.
double currAvg;
for(int i=0; i<A.length-2; i++){
/**
* We check first the two-element slice
*/
currAvg = ((double)(A[i] + A[i+1]))/2;
if(currAvg < minAvgVal){
minAvgVal = currAvg;
minAvgIdx = i;
}
/**
* We check the three-element slice
*/
currAvg = ((double)(A[i] + A[i+1] + A[i+2]))/3;
if(currAvg < minAvgVal){
minAvgVal = currAvg;
minAvgIdx = i;
}
}
/**
* Now we have to check the remaining two elements of the array
* Inside the for we checked ALL the three-element slices (the last one
* began at A.length-3) and all but one two-element slice (the missing
* one begins at A.length-2).
*/
currAvg = ((double)(A[A.length-2] + A[A.length-1]))/2;
if(currAvg < minAvgVal){
minAvgVal = currAvg;
minAvgIdx = A.length-2;
}
return minAvgIdx;
}
}
function result: 3
function result: 0
function result: 1
class Solution {
public int solution(int[] A) {
int minAvgIdx=0;
double minAvgVal=(A[0]+A[1])/2; //At least two elements in A.
double currAvg;
for(int i=0; i<A.length-2; i++){
/**
* We check first the two-element slice
*/
currAvg = ((double)(A[i] + A[i+1]))/2;
if(currAvg < minAvgVal){
minAvgVal = currAvg;
minAvgIdx = i;
}
/**
* We check the three-element slice
*/
currAvg = ((double)(A[i] + A[i+1] + A[i+2]))/3;
if(currAvg < minAvgVal){
minAvgVal = currAvg;
minAvgIdx = i;
}
}
/**
* Now we have to check the remaining two elements of the array
* Inside the for we checked ALL the three-element slices (the last one
* began at A.length-3) and all but one two-element slice (the missing
* one begins at A.length-2).
*/
currAvg = ((double)(A[A.length-2] + A[A.length-1]))/2;
if(currAvg < minAvgVal){
minAvgVal = currAvg;
minAvgIdx = A.length-2;
}
return minAvgIdx;
}
}
function result: 3
function result: 0
function result: 1
class Solution {
public int solution(int[] A) {
int minAvgIdx=0;
double minAvgVal=(A[0]+A[1])/2; //At least two elements in A.
double currAvg;
for(int i=0; i<A.length-2; i++){
/**
* We check first the two-element slice
*/
currAvg = ((double)(A[i] + A[i+1]))/2;
if(currAvg < minAvgVal){
minAvgVal = currAvg;
minAvgIdx = i;
}
/**
* We check the three-element slice
*/
currAvg = ((double)(A[i] + A[i+1] + A[i+2]))/3;
if(currAvg < minAvgVal){
minAvgVal = currAvg;
minAvgIdx = i;
}
}
/**
* Now we have to check the remaining two elements of the array
* Inside the for we checked ALL the three-element slices (the last one
* began at A.length-3) and all but one two-element slice (the missing
* one begins at A.length-2).
*/
currAvg = ((double)(A[A.length-2] + A[A.length-1]))/2;
if(currAvg < minAvgVal){
minAvgVal = currAvg;
minAvgIdx = A.length-2;
}
return minAvgIdx;
}
}
The solution obtained perfect score.