Sort by

recency

|

963 Discussions

|

  • + 0 comments

    import java.io.; import java.util.; import java.util.stream.*;

    class Result {

    /*
     * Complete the 'stones' function below.
     *
     * The function is expected to return an INTEGER_ARRAY.
     * The function accepts following parameters:
     *  1. INTEGER n
     *  2. INTEGER a
     *  3. INTEGER b
     */
    
    public static List<Integer> stones(int n, int a, int b) {
        Set<Integer> results = new TreeSet<>();
        for (int i = 0; i < n; i++) {
            int value = i * b + (n - 1 - i) * a;
            results.add(value);
        }
        return new ArrayList<>(results);
    }
    

    }

    public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int T = Integer.parseInt(bufferedReader.readLine().trim());
    
        for (int tItr = 0; tItr < T; tItr++) {
            int n = Integer.parseInt(bufferedReader.readLine().trim());
            int a = Integer.parseInt(bufferedReader.readLine().trim());
            int b = Integer.parseInt(bufferedReader.readLine().trim());
    
            List<Integer> result = Result.stones(n, a, b);
    
            bufferedWriter.write(
                result.stream()
                    .map(Object::toString)
                    .collect(Collectors.joining(" "))
                + "\n"
            );
        }
    
        bufferedReader.close();
        bufferedWriter.close();
    }
    

    }

  • + 0 comments

    C++ ans

    vector stones(int n, int a, int b) { if (n <= 1) { return {0}; }

    set<int> current;
    
    for (int i = 0; i < n; i++) {
        int value = i * a + (n - 1 - i)*b; // i times a and (n-1-i) times b
        current.insert(value);
    }
    vector<int> result(current.begin(), current.end());
    
    return result;
    

    }

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