Given two integers n and g, the task is to generate an increasing sequence of n integers such that:
- The gcd of all the elements of the sequence is g.
- And, the sum of all the elements is minimum among all possible sequences.
Examples:
Input: n = 6, g = 5
Output: 5 10 15 20 25 30Input: n = 5, g = 3
Output: 3 6 9 12 15
Approach: The sum of the sequence will be minimum when the sequence will consist of the elements:
g, 2 * g, 3 * g, 4 * g, ….., n * g.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to print the required sequence void generateSequence(int n, int g) { for (int i = 1; i <= n; i++) cout << i * g << " "; } // Driver Code int main() { int n = 6, g = 5; generateSequence(n, g); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to print the required sequence static void generateSequence(int n, int g) { for (int i = 1; i <= n; i++) System.out.print(i * g + " ");; } // Driver Code public static void main(String []args) { int n = 6, g = 5; generateSequence(n, g); } } // This code is contributed by Rituraj Jain |
Python3
# Python3 implementation of the approach # Function to print the required sequence def generateSequence(n, g): for i in range(1, n + 1): print(i * g, end = " ") # Driver Code if __name__ == "__main__": n, g = 6, 5 generateSequence(n, g) # This code is contributed by Rituraj Jain |
C#
// C# implementation of the approach using System ; class GFG { // Function to print the required sequence static void generateSequence(int n, int g) { for (int i = 1; i <= n; i++) Console.Write(i * g + " "); } // Driver Code public static void Main() { int n = 6, g = 5; generateSequence(n, g); } } // This code is contributed by Ryuga |
PHP
<?php // PHP implementation of the approach // Function to print the required sequence function generateSequence($n, $g) { for ($i = 1; $i <= $n; $i++) echo $i * $g . " "; } // Driver Code $n = 6; $g = 5; generateSequence($n, $g); // This code is contributed by ita_c ?> |
5 10 15 20 25 30
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Number of ways to divide string in sub-strings such to make them in lexicographically increasing sequence
- Count the number of non-increasing subarrays
- Count permutations that are first decreasing then increasing.
- Increasing permutation of first N natural numbers
- Check if an array is increasing or decreasing
- Generate an alternate increasing and decreasing Array
- Sum of array elements that is first continuously increasing then decreasing
- Print all non-increasing sequences of sum equal to a given number x
- Check if N and M can be made equal by increasing N by A and decreasing M by B
- Sum of all differences between Maximum and Minimum of increasing Subarrays
- Number of Permutations such that no Three Terms forms Increasing Subsequence
- Print all increasing sequences of length k from first n natural numbers
- Minimum size Subarray with maximum sum in non-increasing order
- Minimum steps for increasing and decreasing Array to reach either 0 or N
- Print all numbers in given range having digits in strictly increasing order
- Generate k digit numbers with digits in strictly increasing order
- Count of numbers whose sum of increasing powers of digits is equal to the number itself
- Minimum increment operations to make the array in increasing order
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Minimum value of X to make all array elements equal by either decreasing or increasing by X
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

