Question

Given an array of integer numbers, write a linear running time complexity program in Java to...

Given an array of integer numbers, write a linear running time complexity program in Java to find the stability index in the given input array.

For an array A consisting n integers elements, index i is a stability index in A if

A[0] + A[1] + ... + A[i-1] = A[i+1] + A[i+2] + ... + A[n-1]; where 0 < i < n-1.

Similarly, 0 is an stability index if (A[1] + A[2] + ... + A[n-1]) = 0 and n-1 is an stability index if (A[0] + A[1] + ... + A[n-2]) = 0

Example: Consider the array A = {0, -3, 5, -4, -2, 3, 1, 0}. The stability index found at index 0, 3 and 7.

Important Notes: You must add the main method in your program in order to test your implementation. There are no data errors that need to be checked as all the data will be assumed correct. You can use the array of the previous example to test your program, however, I suggest that you also use other input arrays to validate the correctness and efficiency of your solution. Your program must be submitted only in source code form (.java file). A program that does not compile or does not run loses all points.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


import java.util.ArrayList;
public class StabilityIndex {
  
    /**
     * Time Complexity for the method is N + N = O(n)
     * @param list Excepts an array[] of integers.
     * @return      Return an ArrayList of found stability index(s).
     */
    static ArrayList<Integer> stability_index(int[] list){
        // Store Stability index(s)
        ArrayList<Integer> stabilityList = new ArrayList<>();
      
        // Sum the right side of the array from A[1] - A[n-1] O(n)
        int sum_right =0;
        for (int i = 1; i < list.length; i++) {
            sum_right += list[i];
        }
        //Check if the sum from A[1] - A[n-1] is equal to zero
        if(sum_right == 0){
            stabilityList.add(sum_right);
        }
          
        // Similarly, 0 is an stability index if...
        // (A[1] + A[2] + ... + A[n-1]) = 0 and n-1 is an stability index if...
        // (A[0] + A[1] + ... + A[n-2]) = 0
        // O(n)
        int right = sum_right; int left = 0;
        for (int i = 0; i < list.length - 1; i++) {
          
            int point = i;
            int lead = point +1;
          
            // Start at the second index and continue until the length
            // This side will shrink unit n-2
            right -= (list[lead]);
          
            // Start at the first index and grow
            // This side will grow unil n-1
            left += list[point];
          
            //If right and left are equal then stability index found
            if(right == left){
                stabilityList.add(lead);
            }
        }
        return stabilityList;
    }
  
    static void print(ArrayList<Integer> list){
        list.forEach((integer) -> {
            System.out.println("Stability Index is: " + integer);
        });
        System.out.println("--------------------------- ");
    }
  
    public static void main(String[] args) {
        //UNIT TESTING / TEST CASES
        int[] list = {0, -3, 5, -4, -2, 3, 1, 0};
        //Stability Index is: 0
        //Stability Index is: 3
        //Stability Index is: 7
      
        int[] list1 = {0, 1, -1, 2, -2, 1, -1, 0};
        //Stability Index is: 0
        //Stability Index is: 7
      
        int[] list2 = {10, 2, 3, 4, 3};
        //Stability Index is: 1
      
        int[] list3 = {0, -2, -3, 2, 3, 2, 3, -2, -3, 0};
        //Stability Index is: 0
        //Stability Index is: 9
      
        System.out.println("L1: {0, -3, 5, -4, -2, 3, 1, 0}");
        print(stability_index(list));
        System.out.println("L2: {0, 1, -1, 2, -2, 1, -1, 0}");
        print(stability_index(list1));
        System.out.println("L3: {10, 2, 3, 4, 3}");
        print(stability_index(list2));
        System.out.println("L4: {0, -2, -3, 2, 3, 2, 3, -2, -3, 0}");
        print(stability_index(list3));
        }
    }


Add a comment
Know the answer?
Add Answer to:
Given an array of integer numbers, write a linear running time complexity program in Java to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java question Given an array of integer numbers, write a linear running time complexity program in...

    Java question Given an array of integer numbers, write a linear running time complexity program in Java to find the stability index in the given input array. For an array A consisting n integers elements, index i is a stability index in A itf ATO] + A[1] + +A[iI-1] Ali+1]+ Ali+2] +... + A[n-1]; where 0 <i< n-1 Similarly, 0 is an stability index if (A[1] A[2]A[n-1]) 0 and n-1 is an stability index if (A[0] A[1]+... A[n-21) 0 Example:...

  • 1. Write a Java program to implement Counting Sort and write a driver to test it....

    1. Write a Java program to implement Counting Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 2. Write a Java program to implement Bucket Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 3. In...

  • IN JAVA please Given a sorted array and a target value, return the index if the...

    IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...

  • Write a Java program that generates an array of Fibonacci numbers. Specifications: The program -Fills a...

    Write a Java program that generates an array of Fibonacci numbers. Specifications: The program -Fills a one-dimensional array with the first 30 Fibonacci numbers using a calculation to generate the numbers. Note: The first two Fibonacci numbers 0 and 1 should be generated explicitly as in -long[] series = new long[limit]; //create first 2 series elements series[0] = 0; series[1] = 1; -But, it is not permissible to fill the array explicitly with the Fibonacci series’ after the first two...

  • Java Array The method rotateLeft() takes in a reference a to an integer array and a...

    Java Array The method rotateLeft() takes in a reference a to an integer array and a positive integer n and it returns a new array whose contents is the contents of the input array rotated to the left n places. So each element a[i] of the input array should be placed at location b[i-n] of the returned array. If the index i-n is negative, then that index should "wrap" around to the end of the output array. For example, if...

  • Suppose we have two integer arrays with the same type, write an AL program to check...

    Suppose we have two integer arrays with the same type, write an AL program to check whether or not there are two integers, one from each array, with sum equal to zero. If there are such integers exist, print out all such combinations to the console window, otherise, print out "No integers in these two arrays, one from each array, with sum equal to zero." to the console window. December 3. 2018 For example, suppose we have the following two...

  • Problem Description proving program correctness Consider the following program specification: Input: An integer n > 0...

    Problem Description proving program correctness Consider the following program specification: Input: An integer n > 0 and an array A[0..(n - 1)] of n integers. Output: The smallest index s such that A[s] is the largest value in A[0..(n - 1)]. For example, if n = 9 and A = [ 4, 8, 1, 3, 8, 5, 4, 7, 2 ] (so A[0] = 4, A[1] = 8, etc.), then the program would return 1, since the largest value in...

  • (a) Write a program in Java to implement a recursive search function int terSearch(int A[], int...

    (a) Write a program in Java to implement a recursive search function int terSearch(int A[], int l, int r, int x) that returns the location of x in a given sorted array of n integers A if x is present, otherwise -1. The terSearch search function, unlike the binary search, must consider two dividing points int d1 = l + (r - l)/3 int d2 = d1 + (r - l)/3 For the first call of your recursive search function...

  • write a java program Accept a positive integer n from keyboard and then create an array...

    write a java program Accept a positive integer n from keyboard and then create an array or arraylist containing n random elements within the range [-n,n). Print out the random array or arraylist, and then find out and print out the number of inversions and the maximum subarray (index range of the maximum subarray along with the maximum subarray sum) in the array or arraylist using divide and conquer, respectively. For example, suppose we accept integer 6 from keyboard, then...

  • In Java write the following array- processing methods into the same application, Lab13.java. Use the main...

    In Java write the following array- processing methods into the same application, Lab13.java. Use the main method in this application to test your methods. 1) Write the void method, shiftLeft, which accepts an array of int and changes the elements of this array so that the index of each element is now less by one and the last element of the array is now zero. For example, if the parameter array is {7, 3, 2, 9, 5}, then when this...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT