Sort by

recency

|

47 Discussions

|

  • + 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

  • + 1 comment

    Javascript

    function kFactorization(n, A) {
        // Write your code here
        A.sort((a,b)=>a-b);
        function result(e,f,A){
            if (e==1){
                f.push(1);
                return f;
            }
            if (A.length==0){
                return [-1];
            }
            if (e % A[A.length -1]==0){
                f.push(e);
                e = e / A[A.length -1];
            }
            else{
                A.pop();      
            }
            return result(e,f,A);
        }
          let res = [];
          res = result(n,[],A);
          res.sort((a,b)=>a-b);
         // console.log(res);
          return res;
    
    }