# Function value return

• Given a sorted array (e.g., x= [-2 , 0 ,2]) and a quadratic equation y= ax^2 + bx + c (a, b, c are integer constants, and a != 0). Return the y array in ascending order.

``````public class solution{
public int[] solve(int[] nums, int a, int b, int c){
if(nums == null || nums.length == 0)
return null;
int length = nums.length;
int leftIndex = 0;
int rightIndex = length - 1;
#assume that a is less than 0
int[] result = new int[length];
int resultIndex = 0;

#if a < 0
while(leftIndex <= rightIndex){
int leftValue = a * nums[leftIndex] * nums[leftIndex] + b * nums[leftIndex] + c;
int rightValue = a * nums[rightIndex] * nums[rightIndex] + b * nums[rightIndex] + c;
if(leftValue < rightValue){
result[resultIndex] = leftValue;
leftIndex++;
}
else{
result[resultIndex] = rightValue;
rightIndex--;
}
resultIndex++
}

#if a > 0
#result index is from length -1, and set the value from larger to smaller
}
}
``````

• Can't you just use a priority queue to do this? The solution would be worst case O(nlogn + n). Like so:

``````import heapq
class Solution(object):
def solve(self, xNums, a, b, c):

A = []
heapq.heapify(A)
for x in xNums:
y = a * math.pow(x, 2) + (b * x) + c
heapq.heappush(A, y)

while len(A) > 0:
print heapq.heappop(A)
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.