Sort by

recency

|

48 Discussions

|

  • + 0 comments

    My answer in Typescript, note includes.

    note: it is a litte confuse when Q ask me to print -1 but my code required to return [-1] insteads

    function kFactorization(n: number, A: number[]): number[] {
        /**
         * ya, every recursive function need a memorize
         */
        const memorize: { [n: number]: number[] } = {}
    
        /**
         * recursive function calculate reverseFactorization
         * 
         * + number [n] is number need to calculate
         * -> return [n*] that is smallest possible way to go, until 1
         */
        const reverseFactorization = (n: number): number[] => {
            // way end. return itseft
            if (n == 1) return [n]
    
            // already memorized, skip calculation
            if (n in memorize) return memorize[n]
    
            // calculate all possible way can go, only get the sortest way and "lexicographically"
            // memorized
            return memorize[n] = A.filter(a => n % a == 0)
                .map(b => [...reverseFactorization(n / b), n])
                .sort((a, b) => {
                    // the sorter length
                    if (a.length != b.length) return a.length - b.length
                    // the top lexicographically
                    else {
                        for (let i = 0; i < a.length; i++) {
                            if (a[i] < b[i]) return -1;
                            if (a[i] > b[i]) return 1;
                        }
                        return 0
                    }
                }).shift() ?? []
        }
    
        // doing recursive reverseFactorization with stating is [n]
        let rs = reverseFactorization(n)
    
        // return
        if (rs.length == 0 || rs[0] != 1) return [-1]
        return rs
    }
    
  • + 0 comments

    Simple solution

    vector<int> kFactorization(int n, vector<int> A) {
        // sort A
        sort(A.begin(), A.end());
        // initialize answer vector
        vector<int> ans;
        // loop
        int i = A.size() - 1;
        while (n != 1 && i >= 0){
            while (n % A[i] == 0){
                ans.push_back(n);
                n = n / A[i];
            }
            i--;
        }
        // if no way to reach N print -1
        if (n != 1){
            ans.clear();
            ans.push_back(-1);
            return ans;
        }
        // else return answer
        ans.push_back(1);
        reverse(ans.begin(), ans.end());
        return ans;
    }
    
  • + 0 comments

    Find the factor

  • + 1 comment

    I tried using Greedy approach and it passes all the tests (java)

    public static List<Integer> kFactorization(int n, List<Integer> nums) {
    		//List for storing the divisors of the numbers
    		List<Integer> ansNums = new ArrayList<>();
    		//Sort the nums in reverse order for minimum no of steps for reaching the number n
    		Collections.sort(nums, Collections.reverseOrder());
    		//initialize the flag to true
    		boolean isDiv = true;
    		//keep looping until we reached num from n to 1 and in each iteration number was divided.
    		while (isDiv && n >= 1) {
    			isDiv = false;	//set the flag to false to check if there was a divisor
    			for (int num : nums) {
    				if (n % num == 0) {	//if divisor found
    					ansNums.add(num);
    					n = n / num;
    					isDiv = true;	//set the flag true
    				}
    			}
    		}
    		Collections.sort(ansNums);	//sort the list bcz we added it in reverse order
    		List<Integer> ans = new ArrayList<>();
    		if (ansNums.isEmpty() || n != 1) {	//if we didn't reach the number, return -1
    			ans.add(-1);
    			return ans;
    		} else {
    			ans.add(1);	//else add initial 1 
    			//Multiply the number with divisors and add to the final list
    			for (int num : ansNums) {
    				ans.add(ans.get(ans.size() - 1) * num);
    			}
    			return ans;
    		}
    	}
    
  • + 0 comments

    Here is K Factorization problem solution in Python Java c++ and c programming - https://programs.programmingoneonone.com/2021/07/hackerrank-k-factorization-problem-solution.html