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 {

//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;
}

    public int[] sortTransformedArray_naive(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;
    }

}

@@ -595,16 +595,60 @@
 _360 %7B%0A%0A
+    public static class Solution1 %7B%0A        
 //credit
@@ -755,16 +755,20 @@
 ion%0A    
+   
+ 
 //in sum
@@ -834,16 +834,24 @@
 s logic%0A
+        
 public i
@@ -907,24 +907,32 @@
 b, int c) %7B%0A
+        
     int n = 
@@ -944,16 +944,24 @@
 length;%0A
+        
     int%5B
@@ -983,16 +983,24 @@
 int%5Bn%5D;%0A
+        
     int 
@@ -1002,24 +1002,32 @@
  int i = 0;%0A
+        
     int j = 
@@ -1033,16 +1033,24 @@
  n - 1;%0A
+        
     int 
@@ -1073,24 +1073,32 @@
  n - 1 : 0;%0A
+        
     while (i
@@ -1114,16 +1114,24 @@
         
+     
+   
 if (a %3E=
@@ -1132,24 +1132,32 @@
  (a %3E= 0) %7B%0A
+        
             
@@ -1173,16 +1173,40 @@
 dex--%5D =
+%0A                       
  functio
@@ -1258,32 +1258,61 @@
 , c) ? function(
+%0A                            
 nums%5Bi++%5D, a, b,
@@ -1339,32 +1339,40 @@
 j--%5D, a, b, c);%0A
+        
         %7D else %7B
@@ -1376,32 +1376,40 @@
 e %7B%0A            
+     
+   
 sorted%5Bindex++%5D 
@@ -1409,16 +1409,40 @@
 dex++%5D =
+%0A                       
  functio
@@ -1494,32 +1494,61 @@
 , c) ? function(
+%0A                            
 nums%5Bj--%5D, a, b,
@@ -1595,38 +1595,238 @@
     
+     
+   
 %7D%0A    
-%7D%0A    return sorted;%0A%7D%0A%0A
+        %7D%0A            return sorted;%0A        %7D%0A%0A        private int function(int num, int a, int b, int c) %7B%0A            return a * (num * num) + b * num + c;%0A        %7D%0A    %7D%0A%0A    public static class Solution2 %7B%0A    
     
@@ -1862,14 +1862,8 @@
 rray
-_naive
 (int
@@ -1898,24 +1898,28 @@
 ) %7B%0A        
+ 
+   
 int%5B%5D result
@@ -1943,16 +1943,20 @@
 ength%5D;%0A
+    
         
@@ -2007,16 +2007,20 @@
         
+ 
+   
 result%5Bi
@@ -2043,32 +2043,36 @@
 s%5Bi%5D, a, b, c);%0A
+    
         %7D%0A      
@@ -2073,16 +2073,20 @@
         
+    
 Arrays.s
@@ -2098,16 +2098,20 @@
 esult);%0A
+    
         
@@ -2129,23 +2129,31 @@
 lt;%0A    
-%7D%0A%0A
+    %7D%0A%0A   
     
+ 
 private 
@@ -2197,32 +2197,36 @@
 nt c) %7B%0A        
+    
 return a * (num 
@@ -2243,21 +2243,31 @@
  * num + c;%0A
+        %7D%0A
     %7D%0A%0A%7D%0A

refactor 360