Question: Given an array a that contains only the numbers 1 and 1-. Define - an inverse pair is a pair of indices i and j

Given an array a that contains only the numbers 1 and 1-. Define - an inverse pair is a pair of indices i and j such that i < j and a[i] = -1 and a[j] = 1 or a[i] = 1 and a[j] = -1. Write a method whose signature - public int howManyOpposites(int[] a) The method will receive as a parameter an array filled with the numbers 1 and 1- and will return the number of inverted pairs present in the array. For example, for the following array: -1 1 -1 -1 1 1 The method will return 9, because there are nine inverted pairs: (0, 1), (0, 4), (0, 5), (1, 2), (1, 3), (2, 4), (2, 5), (3, 4), (3 , 5) Please note - the method should have the best possible runtime efficiency, and the best possible memory efficiency. A method that is not maximally efficient will not receive full points. B. (5 points) What is the runtime efficiency of the method you wrote? Explain your answer.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!