package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/minimum-numbers-of-function-calls-to-make-target-array/
 */
public class Problem1558 {
    public int minOperations(int[] nums) {
        int answer = nums.length;
        int max = Arrays.stream(nums).max().getAsInt();
        int val = (int) (Math.log(max) / Math.log(2));
        answer += val;
        answer += max % val;
        for (int num : nums) {
            if (num == 0) {
                answer--;
                continue;
            }
            if (num == 1 || num == max) {
                continue;
            }
            if (num % 2 != 0) {
                answer++;
            }
        }
        return answer;
    }

    public static void main(String[] args) {
        Problem1558 prob = new Problem1558();
        System.out.println(prob.minOperations(new int[]{1,5})); // 5
        System.out.println(prob.minOperations(new int[]{2,2})); // 3
        System.out.println(prob.minOperations(new int[]{4,2,5})); // 6
        System.out.println(prob.minOperations(new int[]{3,2,2,4})); // 7
        System.out.println(prob.minOperations(new int[]{2,4,8,16})); // 8
        System.out.println(prob.minOperations(new int[]{2,3,10})); // 8
        System.out.println(prob.minOperations(new int[]{2,3,10,89})); // 15
    }
}

@@ -230,19 +230,9 @@
 r = 
-nums.length
+0
 ;%0A  
@@ -301,59 +301,84 @@
 int 
-val = (int) (Math.log(max) / Math.log(2));%0A
+m = max;%0A        while (m %3E 1) %7B%0A            if (m %25 2 != 0) %7B%0A     
         
 answ
@@ -377,50 +377,162 @@
     
+   
 answer
- += val;%0A        answer += max %25 val
+++; // plus 1 for any odd numbers%0A            %7D%0A            m /= 2;%0A            answer++; // multiply all by 2%0A        %7D%0A        int maxCount = 0
 ;%0A  
@@ -592,34 +592,8 @@
 ) %7B%0A
-                answer--;%0A
     
@@ -644,24 +644,51 @@
     
-if (num == 1 %7C%7C 
+answer++; // plus 1 from 0%0A            if (
 num 
@@ -709,32 +709,34 @@
             
-continue
+maxCount++
 ;%0A          
@@ -729,38 +729,32 @@
 ++;%0A        
-    %7D%0A
         
     if (num 
@@ -745,28 +745,25 @@
     
-    if (num %25 2 != 0
+if (maxCount == 1
 ) %7B%0A
@@ -782,18 +782,26 @@
     
-answer++;%0A
+    continue;%0A    
     
@@ -810,32 +810,36 @@
       %7D%0A        
+    
 %7D%0A        return
@@ -836,624 +836,216 @@
     
-return answer;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1558 prob = new Problem1558();%0A        System.out.println(prob.minOperations(new int%5B%5D%7B1,5%7D)); // 5%0A        System.out.println(prob.minOperations(new int%5B%5D%7B2,2%7D)); // 3%0A        System.out.println(prob.minOperations(new int%5B%5D%7B4,2,5%7D)); // 6%0A        System.out.println(prob.minOperations(new int%5B%5D%7B3,2,2,4%7D)); // 7%0A        System.out.println(prob.minOperations(new int%5B%5D%7B2,4,8,16%7D)); // 8%0A        System.out.println(prob.minOperations(new int%5B%5D%7B2,3,10%7D)); // 8%0A        System.out.println(prob.minOperations(new int%5B%5D%7B2,3,10,89%7D)); // 15
+    while (num %3E 1) %7B%0A                if (num %25 2 != 0) %7B%0A                    answer++; // plus 1 for any odd numbers%0A                %7D%0A                num /= 2;%0A            %7D%0A        %7D%0A        return answer;
 %0A   

Solve problem 1558