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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started