Consider a non-empty int array ints. A contiguous subarray ints[ start .. start + len -1 ] (with starting index start and length len) is called a flat if all elements of that subarray are equal. Furthermore, such a subarray is called a plateau if it is flat and each of the elements ints[start -1] and ints[start + len] that immediately proceed/succeed the subarray are either nonexistent (i.e., out of array’s index range) or are strictly smaller than the elements of the subarray. Your task includes the design of a public static method longestPlateau(int[] ints) that returns (a compact description of) the longest plateau of the input array ints. You may break ties arbitrarily if ints has more than one longest plateau. The return type should be a 3-element array representing { value , start , len } : The first indicates common element value ints[start] of the plateau, the second its starting index, the third its length. Implement the longestPlateau() method in the provided ArrayLongestPlateau class so that it performs as indicated. The main() method in this class runs some test cases on longestPlateau(). You should also add a few nontrivial and interesting test cases of your own at the end of main()
public class ArrayLongestPlateau {
/**
* longestPlateau() returns the longest plateau of an array of ints.
*
* @return an array int[3] of the form {value, start, len} representing the longest plateau of
* ints[] as a length len contiguous subarray starting at index start with common
* element values value.
*
* For example, on the input array [2, 3, 3, 3, 3, 6, 6, 1, 1, 1], it returns [6, 5, 2],
* indicating the longest plateau of this array is the subarray [6, 6]; it starts at
* index 5 and has length 2.
*
* @param ints
* the input array.
*/
public static int[] longestPlateau(int[] ints) {
// TODO: Replace the following one line stub with your solution. Ours takes linear time and is
// 15 lines long, not counting blank/comment lines or lines already present in this file.
return new int[] { 0, 0, 0 };
}
/**
* main() runs test cases on your longestPlateau() method. Prints summary
* information on basic operations and halts with an error (and a stack
* trace) if any of the tests fail.
*/
public static void main(String[] args) {
String result;
System.out.println("Let's find longest plateaus of arrays!\n");
int[] test1 = { 4, 1, 1, 6, 6, 6, 6, 1, 1 };
System.out.println("longest plateau of " + TestHelper.stringInts(test1) + ":");
result = TestHelper.stringInts(longestPlateau(test1));
System.out.println("[ value , start , len ] = " + result + " \n");
TestHelper.verify(result.equals("[ 6 , 3 , 4 ]"),
"Wrong: that's not the longest plateau!!! No chocolate.");
int[] test2 = { 3, 3, 1, 2, 4, 2, 1, 1, 1, 1 };
System.out.println("longest plateau of " + TestHelper.stringInts(test2) + ":");
result = TestHelper.stringInts(longestPlateau(test2));
System.out.println("[ value , start , len ] = " + result + " \n");
TestHelper.verify(result.equals(
"[ 3 , 0 , 2 ]"),
"Wrong: that's not the longest plateau!!! No chocolate.");
int[] test3 = { 3, 3, 1, 2, 4, 0, 1, 1, 1, 1 };
System.out.println("longest plateau of " + TestHelper.stringInts(test3) + ":");
result = TestHelper.stringInts(longestPlateau(test3));
System.out.println("[ value , start , len ] = " + result + " \n");
TestHelper.verify(result.equals("[ 1 , 6 , 4 ]"),
"Wrong: that's not the longest plateau!!! No chocolate.");
int[] test4 = { 3, 3, 3, 4, 1, 2, 4, 4, 0, 1 };
System.out.println("longest plateau of " + TestHelper.stringInts(test4) + ":");
result = TestHelper.stringInts(longestPlateau(test4));
System.out.println("[ value , start , len ] = " + result + " \n");
TestHelper.verify(result.equals("[ 4 , 6 , 2 ]"),
"Wrong: that's not the longest plateau!!! No chocolate.");
int[] test5 = { 7, 7, 7, 7, 9, 8, 2, 5, 5, 5, 0, 1 };
System.out.println("longest plateau of " + TestHelper.stringInts(test5)
+ ":");
result = TestHelper.stringInts(longestPlateau(test5));
System.out.println("[ value , start , len ] = " + result + " \n");
TestHelper.verify(result.equals("[ 5 , 7 , 3 ]"),
"Wrong: that's not the longest plateau!!! No chocolate.");
int[] test6 = { 4 };
System.out.println("longest plateau of " + TestHelper.stringInts(test6) + ":");
result = TestHelper.stringInts(longestPlateau(test6));
System.out.println("[ value , start , len ] = " + result + " \n");
TestHelper.verify(result.equals("[ 4 , 0 , 1 ]"),
"Wrong: that's not the longest plateau!!! No chocolate.");
int[] test7 = { 4, 4, 4, 5, 5, 5, 6, 6 };
System.out.println("longest plateau of " + TestHelper.stringInts(test7) + ":");
result = TestHelper.stringInts(longestPlateau(test7));
System.out.println("[ value , start , len ] = " + result + " \n");
TestHelper.verify(result.equals("[ 6 , 6 , 2 ]"),
"Wrong: that's not the longest plateau!!! No chocolate.");
System.out.println("\nAdditional tests done by the student or TA:\n");
// Insert your additional test cases here.
}
}Program:
ArrayLongestPlateau.java
public class ArrayLongestPlateau { /** * longestPlateau() returns the longest plateau of an array of ints. * * @return an array int[3] of the form {value, start, len} representing the longest plateau of * ints[] as a length len contiguous subarray starting at index start with common * element values value. * * For example, on the input array [2, 3, 3, 3, 3, 6, 6, 1, 1, 1], it returns [6, 5, 2], * indicating the longest plateau of this array is the subarray [6, 6]; it starts at * index 5 and has length 2. * * @param ints * the input array. */ public static int[] longestPlateau(int[] ints) { final int noDuplicateFreq = 1; // since no duplicates, frequency = 1 int value = ints[0]; // initial value is the first element of ints int start = 0; int frequency = 1; int bestFrequency = 0; int index = 0; // dummy variable created to take the value i + 1 in the first for loop int lastValue = ints[0]; // to keep track of previous elements int lastIndex = 0; boolean condition = false; // condition that satisfies longest plateau when there are repeated integers int max = ints[0]; for(int i = 0; i < ints.length - 1; i++) //This loops traverses all the elements in ints except when length of ints is 1 { if(ints[i] == ints[i + 1]) { frequency++; // adds one since it spotted a same number index = i + 1; if((i == 0 || lastValue <= ints[i]) && (index == ints.length - 1 || ints[index] > ints[index + 1]) && frequency > bestFrequency) // condition for having a longest plateau. Also checking the frequency to ensure that we have the longest sub-array { condition = true; if(lastIndex != 0) // lastIndex is assigned to 0 initially so if sub-array that satisfies condition of plateau starts at index 0 (i.e the first element), no need to change lastIndex { start = lastIndex + 1; } bestFrequency = frequency; value = ints[i]; } } else { lastValue = ints[i]; lastIndex = i; frequency = 1; } } for (int i = 1; !condition && i < ints.length && ints.length > 1; i++) // this loop finds the longest plateau when there are no duplicate (repeated) integers in ints { if (Math.max(max, ints[i]) > max) // to find the greatest element in ints { max = ints[i]; value = max; start = i; bestFrequency = noDuplicateFreq; // since no duplicates, frequency = 1 } else { value = max; bestFrequency = noDuplicateFreq; } } if (ints.length == 1) // to handle special case. When length of ints is 1 { bestFrequency = noDuplicateFreq; } int[] result = {value, start, bestFrequency}; return result; } /** * main() runs test cases on your longestPlateau() method. Prints summary * information on basic operations and halts with an error (and a stack * trace) if any of the tests fail. */ public static void main(String[] args) { String result; System.out.println("Let's find longest plateaus of arrays!\n"); int[] test1 = { 4, 1, 1, 6, 6, 6, 6, 1, 1 }; System.out.println("longest plateau of " + TestHelper.stringInts(test1) + ":"); result = TestHelper.stringInts(longestPlateau(test1)); System.out.println("[ value , start , len ] = " + result + " \n"); TestHelper.verify(result.equals("[ 6 , 3 , 4 ]"), "Wrong: that's not the longest plateau!!! No chocolate."); int[] test2 = { 3, 3, 1, 2, 4, 2, 1, 1, 1, 1 }; System.out.println("longest plateau of " + TestHelper.stringInts(test2) + ":"); result = TestHelper.stringInts(longestPlateau(test2)); System.out.println("[ value , start , len ] = " + result + " \n"); TestHelper.verify(result.equals( "[ 3 , 0 , 2 ]"), "Wrong: that's not the longest plateau!!! No chocolate."); int[] test3 = { 3, 3, 1, 2, 4, 0, 1, 1, 1, 1 }; System.out.println("longest plateau of " + TestHelper.stringInts(test3) + ":"); result = TestHelper.stringInts(longestPlateau(test3)); System.out.println("[ value , start , len ] = " + result + " \n"); TestHelper.verify(result.equals("[ 1 , 6 , 4 ]"), "Wrong: that's not the longest plateau!!! No chocolate."); int[] test4 = { 3, 3, 3, 4, 1, 2, 4, 4, 0, 1 }; System.out.println("longest plateau of " + TestHelper.stringInts(test4) + ":"); result = TestHelper.stringInts(longestPlateau(test4)); System.out.println("[ value , start , len ] = " + result + " \n"); TestHelper.verify(result.equals("[ 4 , 6 , 2 ]"), "Wrong: that's not the longest plateau!!! No chocolate."); int[] test5 = { 7, 7, 7, 7, 9, 8, 2, 5, 5, 5, 0, 1 }; System.out.println("longest plateau of " + TestHelper.stringInts(test5) + ":"); result = TestHelper.stringInts(longestPlateau(test5)); System.out.println("[ value , start , len ] = " + result + " \n"); TestHelper.verify(result.equals("[ 5 , 7 , 3 ]"), "Wrong: that's not the longest plateau!!! No chocolate."); int[] test6 = { 4 }; System.out.println("longest plateau of " + TestHelper.stringInts(test6) + ":"); result = TestHelper.stringInts(longestPlateau(test6)); System.out.println("[ value , start , len ] = " + result + " \n"); TestHelper.verify(result.equals("[ 4 , 0 , 1 ]"), "Wrong: that's not the longest plateau!!! No chocolate."); int[] test7 = { 4, 4, 4, 5, 5, 5, 6, 6 }; System.out.println("longest plateau of " + TestHelper.stringInts(test7) + ":"); result = TestHelper.stringInts(longestPlateau(test7)); System.out.println("[ value , start , len ] = " + result + " \n"); TestHelper.verify(result.equals("[ 6 , 6 , 2 ]"), "Wrong: that's not the longest plateau!!! No chocolate."); System.out.println("\nAdditional tests done by the student or TA:\n"); // Insert your additional test cases here. int[] test8 = { 4, 2, 1}; System.out.println("longest plateau of " + TestHelper.stringInts(test8) + ":"); result = TestHelper.stringInts(longestPlateau(test8)); System.out.println("[ value , start , len ] = " + result + " \n"); TestHelper.verify(result.equals("[ 4 , 0 , 1 ]"), "Wrong: that's not the longest plateau!!! No chocolate."); int[] test9 = { 2, 4, 1 }; System.out.println("longest plateau of " + TestHelper.stringInts(test9) + ":"); result = TestHelper.stringInts(longestPlateau(test9)); System.out.println("[ value , start , len ] = " + result + " \n"); TestHelper.verify(result.equals("[ 4 , 1 , 1 ]"), "Wrong: that's not the longest plateau!!! No chocolate."); int[] test10 = { 1,2,3,4,4,5,6,9 }; System.out.println("longest plateau of " + TestHelper.stringInts(test10) + ":"); result = TestHelper.stringInts(longestPlateau(test10)); System.out.println("[ value , start , len ] = " + result + " \n"); TestHelper.verify(result.equals("[ 9 , 7 , 1 ]"), "Wrong: that's not the longest plateau!!! No chocolate."); int[] test11 = {1,1,6,3,8,8,4,5,5,5,1,9,6,7,7}; System.out.println("longest plateau of " + TestHelper.stringInts(test11) + ":"); result = TestHelper.stringInts(longestPlateau(test11)); System.out.println("[ value , start , len ] = " + result + " \n"); TestHelper.verify(result.equals("[ 5 , 7 , 3 ]"), "Wrong: that's not the longest plateau!!! No chocolate."); int[] test12 = {3,3,3,3,1,1,8,2,5,5}; System.out.println("longest plateau of " + TestHelper.stringInts(test12) + ":"); result = TestHelper.stringInts(longestPlateau(test12)); System.out.println("[ value , start , len ] = " + result + " \n"); TestHelper.verify(result.equals("[ 3 , 0 , 4 ]"), "Wrong: that's not the longest plateau!!! No chocolate."); int[] test13 = {3,3,4,5,5,6,1,1}; System.out.println("longest plateau of " + TestHelper.stringInts(test13) + ":"); result = TestHelper.stringInts(longestPlateau(test13)); System.out.println("[ value , start , len ] = " + result + " \n"); TestHelper.verify(result.equals("[ 6 , 5 , 1 ]"), "Wrong: that's not the longest plateau!!! No chocolate."); } }
TestHelper.java
public class TestHelper { /** * stringInts() converts an array of ints to a String. * * @return a String representation of input int array ints[0..n-1] formatted * as "[ ints[0] , ints[1] , ... , ints[n-1] ]". in O(n) time. * * @param ints * the input array. */ public static String stringInts(int[] ints) { /* * To ensure O(n) time, we can use StringBuilder with repeated appends. * If we use String with repeated concatenations, it will take O(n^2) * time, and it will wastefully create O(n) distinct intermediate * strings. This is because String is immutable. But the mutable * StringBuilder is internally implemented as a dynamically expandable * array, representing a character sequence. See the Java API. We prefer * StringBuilder over the older version called StringBuffer. */ StringBuilder s = new StringBuilder("["); for (int i = 0; i < ints.length; i++) { s.append(" " + ints[i] + " ,"); } int j = s.lastIndexOf(","); // replace the last "," with "]" s.deleteCharAt(j); s.append("]"); return s.toString(); // String representation of character sequence s } /** * verify() checks an invariant and prints an error message if it fails. If invariant is true, * this method does nothing. If invariant is false, the message is printed, followed by a dump * of the program stack. * * @param invariant * the condition to be verified * @param message * the error message to be printed if the invariant fails to hold true. */ public static void verify(boolean invariant, String message) { if (!invariant) { System.out.println("*** ERROR: " + message); Thread.dumpStack(); System.out.println("\n"); } } }
output:
Let's find longest plateaus of arrays! longest plateau of [ 4 , 1 , 1 , 6 , 6 , 6 , 6 , 1 , 1 ]: [ value , start , len ] = [ 6 , 3 , 4 ] longest plateau of [ 3 , 3 , 1 , 2 , 4 , 2 , 1 , 1 , 1 , 1 ]: [ value , start , len ] = [ 3 , 0 , 2 ] longest plateau of [ 3 , 3 , 1 , 2 , 4 , 0 , 1 , 1 , 1 , 1 ]: [ value , start , len ] = [ 1 , 6 , 4 ] longest plateau of [ 3 , 3 , 3 , 4 , 1 , 2 , 4 , 4 , 0 , 1 ]: [ value , start , len ] = [ 4 , 6 , 2 ] longest plateau of [ 7 , 7 , 7 , 7 , 9 , 8 , 2 , 5 , 5 , 5 , 0 , 1 ]: [ value , start , len ] = [ 5 , 7 , 3 ] longest plateau of [ 4 ]: [ value , start , len ] = [ 4 , 0 , 1 ] longest plateau of [ 4 , 4 , 4 , 5 , 5 , 5 , 6 , 6 ]: [ value , start , len ] = [ 6 , 6 , 2 ] Additional tests done by the student or TA: longest plateau of [ 4 , 2 , 1 ]: [ value , start , len ] = [ 4 , 0 , 1 ] longest plateau of [ 2 , 4 , 1 ]: [ value , start , len ] = [ 4 , 1 , 1 ] longest plateau of [ 1 , 2 , 3 , 4 , 4 , 5 , 6 , 9 ]: [ value , start , len ] = [ 9 , 7 , 1 ] longest plateau of [ 1 , 1 , 6 , 3 , 8 , 8 , 4 , 5 , 5 , 5 , 1 , 9 , 6 ,7 , 7 ]: [ value , start , len ] = [ 5 , 7 , 3 ] longest plateau of [ 3 , 3 , 3 , 3 , 1 , 1 , 8 , 2 , 5 , 5 ]: [ value , start , len ] = [ 3 , 0 , 4 ] longest plateau of [ 3 , 3 , 4 , 5 , 5 , 6 , 1 , 1 ]: [ value , start , len ] = [ 6 , 5 , 1 ]
Consider a non-empty int array ints. A contiguous subarray ints[ start .. start + len -1...
Implement the squeeze() method in the provided ArraySqueeze class so that it performs as indicated in the comment. The main() method in this class runs some test cases on squeeze(). You should also add a few nontrivial and interesting test cases of your own at the end of main(). package A1; /** * The purpose of this class is to squeeze an array of ints. * * The main method runs some tests. * * @author andy * */ public...
public static int[] collatz(int start, int numIterations) Given integer start and integer numIterations, return an array containing the Collatz sequence beginning with start up to numIterations. The Collatz function is defined by: 3n + 1 if n is odd n/2 if n is even Given start = 7 and numIterations = 3, this method returns [7, 22, 11, 34] TESTING: collatz(7,3) should return {7, 22, 11, 34} collatz(6,0) should return {6} collatz(6, 5) should return {6, 3, 10, 5, 16,...
Question 2: Consider an array of numbers. Give an algorithm that finds the maximum contiguous subarray that is increasing, Example: A = [1, 7, 2, 5, 4, 6, 8, 9, 3, 14, 16, 11, 30]. The maximum contiguous increasing sequence is 4, 6, 8, 9. Please write an explanation of the algorithm in words and state the run time as well.
1. What is the output of the following code segment? int array[] = { 8, 6, 9, 7, 6, 4, 4, 5, 8, 10 }; System.out.println( "Index Value" ); for ( int i = 0; i < array.length; i++ ) System.out.printf( "%d %d\n", i, array[ i ] ); 2. What is the output of the following code segment? char sentence[] = {'H', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u' }; String output = "The sentence...
What is the result of the following program fragment: int[] array = { 1, 4, 3, 6 }; int what = 0; for ( int index=0; index < array.length; index++ ) { what = what + array[ index ] ; } System.out.println( what );
C++ Language
Given an array length 1 or more of ints, return the difference between the largest and smallest values in the array. Examples: largeDiff (410, 3, 5, 6}, 4) - 7 largeDiff ({7, 2, 10, 9}, 4) - 8 largeDiff ({2, 10, 7, 2}, 4) + 8 largeDiff ({2}, 1) = 0 code.cpp + New I Full Screen 2- int largeDiff (const int a[], int n) { 3 4 5}
Specification Copy the program ValleyPeakPlateau.java to your computer and implement the method named valley_peak_plateau(int[]). The static void valley_peak_plateau(int[] a) method prints to the standard output stream the valleys, peaks, and plateaus found in the int[] a parameter. Only arrays having two or more elements are processed. The following are definitions for valley, peak, and plateau. Note: A plateau is when a number occurs three or more consecutive times in an array. note: int[] a ... an array of ints of...
C++ Linux Question : Remove Nth Node from end of list Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Given n will always be valid. (i.e. n is greater than 0) Follow up: Could you do this in one pass? Hint: Maintain two pointers and update one with...
Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array double[10]; c. charl charArray "Computer Science"; None of the above Analyze the following code: class Test public static void main(Stringl] args) System.out.println(xMethod(10); public static int xMethod(int n) System.out.println("int"); return n; public static long xMethod(long n) System.out.,println("long"); return n The program displays int followed by 10 The program displays long followed by 10. The program does not compile. None of the above. tions 3-4 are...
In C++, develop a class that supports array rotation. Rotating an array is an operation where you shift all elements of the array some number of positions left or right, and elements that are shifted off of the left or right end of the array "wrap around" to the right or left end, respectively. For example, if we rotate the array [1, 2, 3, 4, 5] to the right by 1, we get the array [5, 1, 2, 3, 4]....