Increasing sequence with given GCD Last Updated : 23 Jun, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 the minimum among all possible sequences. Examples: Input: n = 6, g = 5 Output: 5 10 15 20 25 30 Input: 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 ?> JavaScript <script> // Javascript implementation of the approach // Function to print the required sequence function generateSequence(n, g) { for (var i = 1; i <= n; i++) { document.write(i*g+" "); } } // Driver Code var n = 6, g = 5; generateSequence(n, g); </script> Output: 5 10 15 20 25 30 Time Complexity: O(n) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Increasing sequence with given GCD S souradeep Follow Improve Article Tags : Mathematical Computer Science Fundamentals DSA GCD-LCM Practice Tags : Mathematical Similar Reads Maximum GCD of N integers with given product Given N integers with unknown values (ai > 0) having product P. The task is to find the maximum possible greatest common divisor of these N integers. Examples: Input : N = 3, P = 24 Output : 2 The integers will have maximum GCD of 2 when a1 = 2, a2 = 2, a3 = 6. Input : N = 2, P = 1 Output : 1 Onl 5 min read Largest subsequence having GCD greater than 1 Given an array, arr[], find the largest subsequence such that GCD of all those subsequences are greater than 1. Examples: Input: 3, 6, 2, 5, 4 Output: 3 Explanation: There are only three elements(6, 2, 4) having GCD greater than 1 i.e., 2. So the largest subsequence will be 3 Input: 10, 15, 7, 25, 9 14 min read Subsequence of size k with maximum possible GCD We are given an array of positive integers and an integer k. Find the maximum possible GCD of a subsequence of size k. Examples: Input : arr[] = [2, 1, 4, 6] k = 3 Output : 2 GCD of [2, 4, 6] is 2 Input : arr[] = [1, 2, 3] k = 3 Output : 1 GCD of [1, 2, 3] is 1 Method 1 Generate all the subsequences 8 min read Find the GCD that lies in given range Given two positive integer a and b and a range [low, high]. The task is to find the greatest common divisor of a and b which lie in the given range. If no divisor exist in the range, print -1.Examples: Input : a = 9, b = 27, low = 1, high = 5 Output : 3 3 is the highest number that lies in range [1, 7 min read Largest Subset with GCD 1 Given n integers, we need to find size of the largest subset with GCD equal to 1. Input Constraint : n <= 10^5, A[i] <= 10^5Examples: Input : A = {2, 3, 5}Output : 3Explanation: The largest subset with a GCD greater than 1 is {2, 3, 5}, and the GCD of all the elements in the subset is 3.Input 6 min read Like