In: Computer Science
You've built an inflight entertainment system with on-demand movie streaming.
Users on longer flights like to start a second movie right when their first one ends, but they complain that the plane usually lands before they can see the ending. So you're building a feature for choosing two movies whose total runtimes will equal the exact flight length.
Write a method that takes an integer flightLength (in minutes) and an array of integers movieLengths (in minutes) and returns a boolean indicating whether there are two numbers in movieLengths whose sum equals flightLength.
When building your method:
==========================
import org.junit.Test;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import static org.junit.Assert.*;
public class Solution {
public static boolean canTwoMoviesFillFlight(int[] movieLengths, int flightLength) {
// determine if two movies add up to the flight length
return false;
}
// tests
@Test
public void shortFlightTest() {
final boolean result = canTwoMoviesFillFlight(new int[] {2, 4},
1);
assertFalse(result);
}
@Test
public void longFlightTest() {
final boolean result = canTwoMoviesFillFlight(new int[] {2, 4},
6);
assertTrue(result);
}
@Test
public void onlyOneMovieHalfFlightLenghtTest() {
final boolean result = canTwoMoviesFillFlight(new int[] {3, 8},
6);
assertFalse(result);
}
@Test
public void twoMoviesHalfFlightLengthTest() {
final boolean result = canTwoMoviesFillFlight(new int[] {3, 8, 3},
6);
assertTrue(result);
}
@Test
public void lotsOfPossiblePairsTest() {
final boolean result = canTwoMoviesFillFlight(new int[] {1, 2, 3,
4, 5, 6}, 7);
assertTrue(result);
}
@Test
public void notUsingFirstMovieTest() {
final boolean result = canTwoMoviesFillFlight(new int[] {4, 3, 2},
5);
assertTrue(result);
}
@Test
public void oneMovieTest() {
final boolean result = canTwoMoviesFillFlight(new int[] {6},
6);
assertFalse(result);
}
@Test
public void noMoviesTest() {
final boolean result = canTwoMoviesFillFlight(new int[] {},
6);
assertFalse(result);
}
public static void main(String[] args) {
Result result = JUnitCore.runClasses(Solution.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
if (result.wasSuccessful()) {
System.out.println("All tests passed.");
}
}
}
=======================
PLEASE EXPLAIN THE LOGIC AND WRITE THE SOLUTION ALSO .
PROGRAMMING LANGUAGE : JAVA
Logic: We can just check the sum of 2 elements in the list if it is equal to given total time using 2 nested for loops. If any such combination is found, then we return true. If no such combination is found and the loop is traversed completely, then we return false. Also as a base case, we check if the number of movies in the list is greater than equal to 2. If it is less than 2, then we return false.
Java Code:
import org.junit.Test;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import static org.junit.Assert.*;
public class Solution {
public static boolean canTwoMoviesFillFlight(int[]
movieLengths, int flightLength) {
// determine if two movies add up to the
flight length
int n = movieLengths.length;
//check if number of movies is greater than 1
if(n < 2) //if less than 2, return false
return false;
//check using 2 for loops for 2 elements having sum =
flightLength
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
//if 2 such
movies are found, then return true
if(movieLengths[i] + movieLengths[j] == flightLength)
return true;
}
}
//if no two such movies are found, then return
false
return false;
}
// tests
@Test
public void shortFlightTest() {
final boolean result = canTwoMoviesFillFlight(new int[] {2, 4},
1);
assertFalse(result);
}
@Test
public void longFlightTest() {
final boolean result = canTwoMoviesFillFlight(new int[] {2, 4},
6);
assertTrue(result);
}
@Test
public void onlyOneMovieHalfFlightLenghtTest() {
final boolean result = canTwoMoviesFillFlight(new int[] {3, 8},
6);
assertFalse(result);
}
@Test
public void twoMoviesHalfFlightLengthTest() {
final boolean result = canTwoMoviesFillFlight(new int[] {3, 8, 3},
6);
assertTrue(result);
}
@Test
public void lotsOfPossiblePairsTest() {
final boolean result = canTwoMoviesFillFlight(new int[] {1, 2, 3,
4, 5, 6}, 7);
assertTrue(result);
}
@Test
public void notUsingFirstMovieTest() {
final boolean result = canTwoMoviesFillFlight(new int[] {4, 3, 2},
5);
assertTrue(result);
}
@Test
public void oneMovieTest() {
final boolean result = canTwoMoviesFillFlight(new int[] {6},
6);
assertFalse(result);
}
@Test
public void noMoviesTest() {
final boolean result = canTwoMoviesFillFlight(new int[] {},
6);
assertFalse(result);
}
public static void main(String[] args) {
Result result = JUnitCore.runClasses(Solution.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
if (result.wasSuccessful()) {
System.out.println("All tests passed.");
}
}
}
Output:
Screenshot of added code: