Quadratic Application - Google Top Interview Questions


Problem Statement :


You are given a list of integers nums sorted in ascending order, and integers a, b, and c. 

Apply the following function for each number x in nums: ax^2 + bx + cax 
2
 +bx+c and return the resulting list in ascending order.

This should be done in \mathcal{O}(n)O(n) time.

Constraints

n ≤ 100,000 where n is the length of nums

Example 1

Input

nums = [-2, 3]

a = 1

b = -3

c = 2

Output

[2, 12]
Explanation

We have


nums[0] = 1*-2**2 + -3*-2 + 2 = 4 + 6 + 2 = 12

nums[1] = 1*3**2 + -3*3 + 2 = 9 + -9 + 2 = 2

After we sort [12, 2], we get [2, 12]



Solution :



title-img




                        Solution in C++ :

vector<int> solve(vector<int>& nums, int a, int b, int c) {
    auto f = [=](int x) { return (a * x + b) * x + c; };
    int n = nums.size();

    vector<int> ans;
    ans.reserve(n);

    // The remaining input to process is the range of indices [i, j)
    int i = 0, j = n;
    while (j > i) {
        // Our two candidates are always on the end of the range.
        int fi = f(nums[i]);
        int fj = f(nums[j - 1]);

        // If `a >= 0`, then `f` is convex-up. In that case the global max (in the range [i, j))
        // is one of the endpoints, and we will eventually converge upon the global min.
        // Otherwise, we symmetrically take the min so as to reach the global max.
        if (a >= 0 ? fi >= fj : fi <= fj) {
            ans.push_back(fi);
            ++i;
        } else {
            ans.push_back(fj);
            --j;
        }
    }

    // If `a >= 0`, then `ans` is decreasing (since we were repeatedly taking the max), so we need
    // to reverse it.
    if (a >= 0) reverse(ans.begin(), ans.end());

    return ans;
}
                    




                        Solution in Python : 
                            
class Solution:
    def solve(self, nums, a, b, c):
        q = deque()

        def cal(x):
            return a * x ** 2 + b * x + c

        l = 0
        r = len(nums) - 1

        is_pos = a >= 0

        while l <= r:
            lv = cal(nums[l])
            rv = cal(nums[r])

            if is_pos:
                if lv >= rv:
                    q.appendleft(lv)
                    l += 1
                else:
                    q.appendleft(rv)
                    r -= 1
            else:
                if lv < rv:
                    q.append(lv)
                    l += 1
                else:
                    q.append(rv)
                    r -= 1

        return list(q)
                    


View More Similar Problems

Find Merge Point of Two Lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share

View Solution →

Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function

View Solution →

Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

View Solution →

Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

View Solution →

Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →

Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →