Sort by

recency

|

947 Discussions

|

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/BXNoCVP0XEE Solution 1 :

    vector<int> stones(int n, int a, int b) {
        if(a > b){
            a = b + a;
            b = a - b;
            a = a - b;
        }
        int step = b - a;
        vector<int> result;
        for(int i = a * (n-1); i < b * (n-1); i += step) result.push_back(i);
        result.push_back(b * (n-1));
        return result;
    }
    

    Solution 2 :

    vector<int> stones(int n, int a, int b) {
        vector<int> result;
        for(int i = min(a, b) * (n-1); i < max(a,b) * (n-1); i += abs(b - a)) result.push_back(i);
        result.push_back(max(a, b) * (n-1));
        return result;
    } 
    
  • + 0 comments

    I think there is something wrong with the examples except for the first one. The first parameter represents non-zero stone. However, in the rest example, including the test cases, n represents total number of stones including non-zero. This is the solution to pass all of the tests.

    public static List<int> Run(int count, int diff1, int diff2)
    {
    	if (diff1 == diff2) return [diff1 * (count - 1)];
    
    	var small = diff1 < diff2 ? diff1 : diff2;
    	var large = diff1 < diff2 ? diff2 : diff1;
    
    	var start = small * (count - 1);
    	var end = large * (count - 1);
    	var step = large - small;
    
    	if (start == 0) start += step;
    
    	var faces = new List<int>();
    
    	for (var i = start; i <= end; i += step)
    	{
    		faces.Add(i);
    	}
    
    	return faces;
    }
    
  • + 0 comments
        public static List<Integer> stones(int n, int a, int b) {
            Set<Integer> resultsSet = new HashSet<>();
            for (int i = 0; i < n; i++) {
                resultsSet.add(i * a + (n - i - 1) * b);
            }
            Integer[] resultsArray = new Integer[resultsSet.size()];
            resultsSet.toArray(resultsArray);
            Arrays.sort(resultsArray);
            return Arrays.asList(resultsArray);
        }
    
  • + 2 comments

    Python3

    def stones(n, a, b):
        # Write your code here
        res=[]
        for i in range(n):
            res.append((i)*b+(n-i-1)*a)
        return sorted(set(res))
    
  • + 0 comments

    for Python3 Platform

    from itertools import combinations_with_replacement
    
    def stones(n, a, b):
        ans = set()
        diff_perm = combinations_with_replacement((a, b), n-1)
        
        for i in diff_perm:
            ans.add(sum(i))
        
            
        return sorted(ans)
    
    T = int(input())
    while (T > 0):
        n = int(input())
        a = int(input())
        b = int(input())
        
        result = stones(n, a, b)
        
        print(*result)
        
        T -= 1