Question: Homework 1 Java Write a method longestPrefix that finds the longest prefix of an array of numbers such that the average of that prefix is
Homework 1 Java
Write a method longestPrefix that finds the longest prefix of an array of numbers such that the average of that prefix is less than an upper bound. Specifically, the method takes an array of floats x and a float upperBound and returns the greatest index i such that elements 0 through i of x have an average less than the upper bound or returns -1 if there is no such index. See LongestPrefix.java for example test cases.
Examples,

public static void main(String[l args) [ System.out.println ("LongestPrefix") float input -[0, 1, 2, 3, 4 test( testNum: 1, input, upperBound: 1.2f, expected: 2): float input -[0, 1, 2, 3, 4 test( testNum: 2, input, upperBound: 1.8f, expected: 3): float input - [4, 3, 2, 1, 0 test( testNum: 3, input, upperBound: 1.2f, expected: -1): float input - [4, 3, 2, 1, 0, 0, 0, 0, 0: test( testNum: 4, input, upperBound: 1.2f, expected: 8): float input -[4, 3, 2, 1, 0, 0, 0, 0, 1: test testNum: 5, input, upperBound: 1.2f, expected: -1): /takes an array of floats x and an upper bound //returns the greatest index i such that elements 0 through i of x have an average less than the upper bound 1or returns -1 if there is no such index public static int longestPrefix(float x[l, float upperBound) [...)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
