Sort by

recency

|

964 Discussions

|

  • + 0 comments

    This is such a needlessly confusing problem. Here are some corrections/clarifications for those that might be confused:

    1. any two consecutive stones' numbers differ by one of two values

      This part is very misleading. "Differ" makes it sound like the values can either increase or decrease from one stone to the next, but they only increase.

    2. int n: the number of non-zero stones

      This is just completely wrong. Actually, n is the total number of stones, including the zero stone.

    3. The first line contains an integer T, the number of test cases.

      Take special note of this. The input format is very confusing because each test case can actually be multiple test cases.

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