package com.fishercoder.solutions;

import java.util.Arrays;

/**
 * 360. Sort Transformed Array
 *
 * Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f(x) = ax2 + bx + c to each element x in the array.

 The returned array must be in sorted order.

 Expected time complexity: O(n)

 Example:
 nums = [-4, -2, 2, 4], a = 1, b = 3, c = 5,

 Result: [3, 9, 15, 33]

 nums = [-4, -2, 2, 4], a = -1, b = 3, c = 5

 Result: [-23, -5, 1, 7]

 Credits:
 Special thanks to @elmirap for adding this problem and creating all test cases.
 */
public class _360 {

    public static class Solution1 {
        //credit: https://discuss.leetcode.com/topic/48424/java-o-n-incredibly-short-yet-easy-to-understand-ac-solution
        //in sum, only two cases: when a >= 0 or when a < 0, this simplifies logic
        public int[] sortTransformedArray(int[] nums, int a, int b, int c) {
            int n = nums.length;
            int[] sorted = new int[n];
            int i = 0;
            int j = n - 1;
            int index = a >= 0 ? n - 1 : 0;
            while (i <= j) {
                if (a >= 0) {
                    sorted[index--] =
                        function(nums[i], a, b, c) >= function(nums[j], a, b, c) ? function(
                            nums[i++], a, b, c) : function(nums[j--], a, b, c);
                } else {
                    sorted[index++] =
                        function(nums[i], a, b, c) >= function(nums[j], a, b, c) ? function(
                            nums[j--], a, b, c) : function(nums[i++], a, b, c);
                }
            }
            return sorted;
        }

        private int function(int num, int a, int b, int c) {
            return a * (num * num) + b * num + c;
        }
    }

    public static class Solution2 {
        public int[] sortTransformedArray(int[] nums, int a, int b, int c) {
            int[] result = new int[nums.length];
            for (int i = 0; i < nums.length; i++) {
                result[i] = function(nums[i], a, b, c);
            }
            Arrays.sort(result);
            return result;
        }

        private int function(int num, int a, int b, int c) {
            return a * (num * num) + b * num + c;
        }
    }

}

@@ -59,527 +59,8 @@
 s;%0A%0A
-/**%0A * 360. Sort Transformed Array%0A *%0A * Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f(x) = ax2 + bx + c to each element x in the array.%0A%0A The returned array must be in sorted order.%0A%0A Expected time complexity: O(n)%0A%0A Example:%0A nums = %5B-4, -2, 2, 4%5D, a = 1, b = 3, c = 5,%0A%0A Result: %5B3, 9, 15, 33%5D%0A%0A nums = %5B-4, -2, 2, 4%5D, a = -1, b = 3, c = 5%0A%0A Result: %5B-23, -5, 1, 7%5D%0A%0A Credits:%0A Special thanks to @elmirap for adding this problem and creating all test cases.%0A */%0A
 publ
@@ -671,32 +671,36 @@
                 
+    
 function(nums%5Bi%5D
@@ -744,32 +744,40 @@
  c) ? function(%0A
+        
                 
@@ -919,32 +919,36 @@
                 
+    
 function(nums%5Bi%5D
@@ -992,32 +992,40 @@
  c) ? function(%0A
+        
                 

refactor 360