Sort by

recency

|

961 Discussions

|

  • + 0 comments

    My solution in C language :

    int* stones(int n, int a, int b, int* result_count)
    {
        int i = 0, c = (a <= b) ? a : b, d = abs(a - b), t = (a != b) ? n : 1;
        
        int * answer = malloc(t * sizeof(int));
            
        *(answer+0) = 0 + c * (n-1);
            
        for (i = 1; i < t; ++i)
            *(answer+i) = *(answer+i-1) + d;
        
        *result_count = t;
            
        return answer;
    }
    

    The solution here is to use the absolute difference between the two differences to increment the initial value (0 + (smallest_value_between_a_and_b * (n-1))) until you reach the expected number of stones (initial value is the first stone).

  • + 0 comments
    public static Set<Integer> stones(int num, int first, int second) {
        Set<Integer> finalset = new TreeSet<>(); 
        helper(num, first, second, finalset);
        helper(num, second, first, finalset);
        return finalset;
    }
    
    private static void helper(int num, int first, int second, Set<Integer> finalset) {
        int d = num-1;
        int x = d;
        int y = 0;
        for(int i=1; i<=d; i++) {
            List<Integer> lst = new ArrayList<>();
            for(int j = 0; j < x; j++) {
                lst.add(lst.size()>0 ? first + lst.get(lst.size()-1) : first);
            }
            for(int j = 0; j< y; j++) {
                lst.add(lst.size()>0 ? second + lst.get(lst.size()-1) : second);
            }
            x--;
            y++;
            finalset.add(lst.get(lst.size()-1));
        }        
    }
    
  • + 0 comments

    We know the output as follow:

    • if n=1 output stone will be (0)
    • if n=2 output stone will be (a, b)
    • if n=3 output stone will be (2a, a+b, 2b)
    • if n=4 output stone will be (3a, 2a+b, a+2b, 3b)
    • if n=5 output stone will be (4a, 3a+b, 2a+2b, a+3b, 4b) final output can be formulated as (n-i-1)*a + i*b ***where i ranges from 0 to n-1* **

    we can iterate over range 'n' and add these values to set remove duplicates.

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

    Here is problem solution in python, Java, c++, C and Javascript - https://programmingoneonone.com/hackerrank-manasa-and-stones-problem-solution.html

  • + 0 comments

    python

    def stones(n, a, b):
        # a_count + b_count = n-1
        answer = []
        if a>b:
            a_count = 0
            b_count = n-1
            while a_count<= n-1:
                tmp = a_count*a+b_count*b
                if tmp not in answer:
                    answer.append(tmp)
                a_count+=1
                b_count-=1
        else:
            a_count = n-1
            b_count = 0
            while a_count>= 0:
                tmp = a_count*a+b_count*b
                if tmp not in answer:
                    answer.append(tmp)
                a_count-=1
                b_count+=1
        return answer