Saturday, 26 August 2023

Codeforces : 

Problem Statement Link -->   Problem - A - Codeforces

Before seeing the code, I suggest you to watch this youtube video to get the Math behind it. 

YouTube Video By cpwithcpp youtube channel

Solution in JAVA:


import java.util.Scanner;

import java.util.ArrayList;


public class test1 {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        int t = scanner.nextInt();

        

        while (t-- > 0) {

            int x = scanner.nextInt();

            int y = scanner.nextInt();

            int n = scanner.nextInt();

            

            ArrayList<Integer> vec = new ArrayList<>();

            

            int c = 1;

            vec.add(y);

            

            for (int i = 1; i < n; i++) {

                vec.add(vec.get(i - 1) - c);

                c++;

            }

            

            if (vec.get(n - 1) >= x) {

                vec.set(n - 1, x);

                

                for (int i = n - 1; i >= 0; i--) {

                    System.out.print(vec.get(i) + " ");

                }

                System.out.println();

                continue;

            }

            

            System.out.println("-1");

        }

        scanner.close();

    }

}


No comments:

Post a Comment

Good thoughtful question on Binary search on answers

Problem link:  https://leetcode.com/problems/maximize-score-of-numbers-in-ranges/description/ Solution: //import java.util.Arrays; class So...