Question

In: Computer Science

java Given a sequence of integers as an array, determine whether it is possible to obtain...

java

Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array. Here are the constraints. 1. You must use only arrays. 2. You are not allowed to make copies of the array (use only the one that is passed). 3. Your algorithm running time must be at most O(n 2 ). i.e. You can have only one loop within another. Ideally, you should be able to find a linear running time algorithm. i.e. you can even solve this problem using a single loop. Note: Sequence a0, a1, . . . an is considered to be a strictly increasing if a0 < a1 < . . . < an. Sequence containing only one element is also considered to be strictly increasing. Assuming your method is almostIncreasingSequence, following main method could be used to test your code.

public static void main(String[] args) { int[] a1 = {1, 3, 2, 1}; // false int[] a2 = {1, 3, 2}; // true int[] a3 = {1, 2, 1, 2}; // false int[] a4 = {3, 6, 5, 8, 10, 20, 15}; // false int[] a5 = {1, 1, 2, 3, 4, 4}; // false int[] a6 = {1, 4, 10, 4, 2}; // false int[] a7 = {10, 1, 2, 3, 4, 5}; // true int[] a8 = {1, 1, 1, 2, 3}; // false int[] a9 = {0, -2, 5, 6}; // true int[] a10 = {1, 2, 3, 4, 5, 3, 5, 6}; // false int[] a11 = {40, 50, 60, 10, 20, 30}; // false int[] a12 = {1, 1}; // true int[] a13 = {1, 2, 5, 3, 5}; // true int[] a14 = {1, 2, 5, 5, 5}; // false int[] a15 = {10, 1, 2, 3, 4, 5, 6, 1}; // false int[] a16 = {1, 2, 3, 4, 3, 6}; // true int[] a17 = {1, 2, 3, 4, 99, 5, 6}; // true int[] a18 = {123, -17, -5, 1, 2, 3, 12, 43, 45}; // true int[] a19 = {3, 5, 67, 98, 3}; // true int[] a20 = {1, 1, 1}; // false System.out.println(Arrays.toString(a1) + ": " + (almostIncreasingSequence(a1) == false)); System.out.println(Arrays.toString(a2) + ": " + (almostIncreasingSequence(a2) == true)); System.out.println(Arrays.toString(a3) + ": " + (almostIncreasingSequence(a3) == false)); System.out.println(Arrays.toString(a4) + ": " + (almostIncreasingSequence(a4) == false)); System.out.println(Arrays.toString(a5) + ": " + (almostIncreasingSequence(a5) == false)); System.out.println(Arrays.toString(a6) + ": " + (almostIncreasingSequence(a6) == false)); System.out.println(Arrays.toString(a7) + ": " + (almostIncreasingSequence(a7) == true)); System.out.println(Arrays.toString(a8) + ": " + (almostIncreasingSequence(a8) == false)); System.out.println(Arrays.toString(a9) + ": " + (almostIncreasingSequence(a9) == true)); System.out.println(Arrays.toString(a10) + ": " + 2 (almostIncreasingSequence(a10) == false)); System.out.println(Arrays.toString(a11) + ": " + (almostIncreasingSequence(a11) == false)); System.out.println(Arrays.toString(a12) + ": " + (almostIncreasingSequence(a12) == true)); System.out.println(Arrays.toString(a13) + ": " + (almostIncreasingSequence(a13) == true)); System.out.println(Arrays.toString(a14) + ": " + (almostIncreasingSequence(a14) == false)); System.out.println(Arrays.toString(a15) + ": " + (almostIncreasingSequence(a15) == false)); System.out.println(Arrays.toString(a16) + ": " + (almostIncreasingSequence(a16) == true)); System.out.println(Arrays.toString(a17) + ": " + (almostIncreasingSequence(a17) == true)); System.out.println(Arrays.toString(a18) + ": " + (almostIncreasingSequence(a18) == true)); System.out.println(Arrays.toString(a19) + ": " + (almostIncreasingSequence(a19) == true)); System.out.println(Arrays.toString(a20) + ": " + (almostIncreasingSequence(a20) == false)); }

Output should be the following (Please carefully look at why it prints all true before asking questions). [1, 3, 2, 1]: true [1, 3, 2]: true [1, 2, 1, 2]: true [3, 6, 5, 8, 10, 20, 15]: true [1, 1, 2, 3, 4, 4]: true [1, 4, 10, 4, 2]: true [10, 1, 2, 3, 4, 5]: true [1, 1, 1, 2, 3]: true [0, -2, 5, 6]: true [1, 2, 3, 4, 5, 3, 5, 6]: true [40, 50, 60, 10, 20, 30]: true [1, 1]: true [1, 2, 5, 3, 5]: true [1, 2, 5, 5, 5]: true [10, 1, 2, 3, 4, 5, 6, 1]: true [1, 2, 3, 4, 3, 6]: true [1, 2, 3, 4, 99, 5, 6]: true [123, -17, -5, 1, 2, 3, 12, 43, 45]: true [3, 5, 67, 98, 3]: true [1, 1, 1]: true

Solutions

Expert Solution

Below is the code for the given problem with comments to explain the code as is.

Code passes all the given test cases

import java.util.Scanner;
import java.util.Arrays;

public class Main
{
   public static void main(String[] args) {
int[] a1 = {1, 3, 2, 1};
int[] a2 = {1, 3, 2}; // true
int[] a3 = {1, 2, 1, 2}; // false
int[] a4 = {3, 6, 5, 8, 10, 20, 15}; // false
int[] a5 = {1, 1, 2, 3, 4,4}; // false
int[] a6 = {1, 4, 10, 4, 2}; // false
int[] a7 = {10, 1, 2,3, 4, 5}; // true
int[] a8 = {1, 1, 1, 2, 3}; // false
int[] a9 = {0,-2, 5, 6}; // true
int[] a10 = {1, 2, 3, 4, 5, 3, 5, 6}; // false
int[] a11 = {40, 50, 60, 10, 20, 30}; // false
int[] a12 = {1, 1}; // true
int[] a13 = {1, 2, 5, 3, 5}; // true
int[] a14 = {1, 2, 5, 5, 5}; //
int[] a15 = {10, 1, 2, 3, 4, 5, 6, 1}; // false
int[] a16 = {1, 2,3, 4, 3, 6}; // true
int[] a17 = {1, 2, 3, 4, 99, 5, 6}; // true
int[] a18 = {123, -17, -5, 1, 2, 3, 12, 43, 45}; // true
int[] a19 = {3, 5,67, 98, 3}; // true
int[] a20 = {1, 1, 1}; // false
  
System.out.println(Arrays.toString(a1) + ": " +
(almostIncreasingSequence(a1) == false));
System.out.println(Arrays.toString(a2) + ": " +
(almostIncreasingSequence(a2) == true));
System.out.println(Arrays.toString(a3) + ": " +
(almostIncreasingSequence(a3) == false));
System.out.println(Arrays.toString(a4) + ": " +
(almostIncreasingSequence(a4) == false));
System.out.println(Arrays.toString(a5) + ": " +
(almostIncreasingSequence(a5) == false));
System.out.println(Arrays.toString(a6) + ": " +
(almostIncreasingSequence(a6) == false));
System.out.println(Arrays.toString(a7) + ": " +
(almostIncreasingSequence(a7) == true));
System.out.println(Arrays.toString(a8) + ": " +
(almostIncreasingSequence(a8) == false));
System.out.println(Arrays.toString(a9) + ": " +
(almostIncreasingSequence(a9) == true));
System.out.println(Arrays.toString(a10) + ": " +
(almostIncreasingSequence(a10) == false));
System.out.println(Arrays.toString(a11) + ": " +
(almostIncreasingSequence(a11) == false));
System.out.println(Arrays.toString(a12) + ": " +
(almostIncreasingSequence(a12) == true));
System.out.println(Arrays.toString(a13) + ": " +
(almostIncreasingSequence(a13) == true));
System.out.println(Arrays.toString(a14) + ": " +
(almostIncreasingSequence(a14) == false));
System.out.println(Arrays.toString(a15) + ": " +
(almostIncreasingSequence(a15) == false));
System.out.println(Arrays.toString(a16) + ": " +
(almostIncreasingSequence(a16) == true));
System.out.println(Arrays.toString(a17) + ": " +
(almostIncreasingSequence(a17) == true));
System.out.println(Arrays.toString(a18) + ": " +
(almostIncreasingSequence(a18) == true));
System.out.println(Arrays.toString(a19) + ": " +
(almostIncreasingSequence(a19) == true));
System.out.println(Arrays.toString(a20) + ": " +
(almostIncreasingSequence(a20) == false));
}
  
   //function to find if it is possible to obtain increasing Sequence by removing
   //an integer
   public static boolean almostIncreasingSequence(int[] array){
   int flag = 0; // flag to keep track of number of integers removed from sequence
  
   // Loop for the array
   // if flag is more than two, our codition fails
   for(int i=0; i<=array.length-3 && flag<3; i++){
   int first = array[i];
   int second = array[i+1];
   int third = array[i+2];
  
   // If first element is greater than second, it is not increasing sequence
   // so it has to be removed
   if(first>=second){
   flag++;
   array[i] = second-1;
   }
  
   // Similarly if second is greater than third, it violates the increasing
   // sequence and has to be removed
   if(second>=third){
   flag++;
   if(first==third){
   array[i+2] = second+1;
   } else {
   array[i+1] = first;
   }
   }
   }
   // If flag is more than one -> we have to remove more than one element
   // to make it increasing sequence
   return flag<=1;
   }
}


Related Solutions

Java Program Sequential Search You are given a sequence of n integers S and a sequence...
Java Program Sequential Search You are given a sequence of n integers S and a sequence of different q integers T. Write a program which outputs C, the number of integers in T which are also in the set S. Do not use any import sort packages. Input: In the first line n is given. In the second line, n integers are given. In the third line q is given. Then, in the fourth line, q integers are given. Output:...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
Using Java please You are given an array of integers arr. Your task is to count...
Using Java please You are given an array of integers arr. Your task is to count the number of contiguous subarrays, such that each element of the subarray appears at least twice. E.g For arr = [0, 0, 0], the output should be duplicatesOnSegment(arr) = 3.
A)Given a generic array with ‘n’ items (not only integers). Give a solution for checking whether...
A)Given a generic array with ‘n’ items (not only integers). Give a solution for checking whether there are any duplicate elements in the array or not? You just need to return a boolean (true or false). State the complexity of your solution. Can you come up with another solution that has O(n logn) complexity? Explain. B) Now consider the same problem as Question A, but this time you only have positive integer numbers ranging from 0 to (n-1) in the...
Given a generic array with ‘n’ items (not only integers). Give a solution for checking whether...
Given a generic array with ‘n’ items (not only integers). Give a solution for checking whether there are any duplicate elements in the array or not? You just need to return a boolean (true or false). State the complexity of your solution. Can you come up with another solution that has O(n logn) complexity? Explain.
Given an array ? of ? integers. Divide ? into three subsets such that the total...
Given an array ? of ? integers. Divide ? into three subsets such that the total absolute difference of subset sums between them is minimum. Provide python source code, time complexity, and pseudocode. thank you
1. Given an array of integers a dimension n. If the array contains the same number...
1. Given an array of integers a dimension n. If the array contains the same number of even and odd elements get (a1 + an) (a2 + an-1) ... 2. Given an array of integers dimension n. All array elements with even numbers preceding the first element to the maximum, multiplied by the maximum. 3. Given an array of dimension n. Insert after each zero element of the element in the middle (or the amount of secondary elements for even...
In Java, a set of integers is given. write a function to find 2 integers in...
In Java, a set of integers is given. write a function to find 2 integers in this set that sums upto a target value. i.e [1,5,2,0,11,3] target = 7 result [5,2] i.e [1,5,4,0,14,-7] target = 9 result [5,4] NOTE: THE SAME INTEGER CANNOT BE USED TWICE !!!
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then prints the following output: a. every element (on a single line) b. every element at an even index (on a single line) c. every even element (on a single line) d. all elements in reverse order (on a single line) e. only the first and last elements (on a single line)
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT