Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Longest sub-sequence with maximum GCD
Next article icon

Longest sub-sequence with maximum GCD

Last Updated : 23 Dec, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array arr[] of length N, the task is to find the length of the longest sub-sequence with the maximum possible GCD.
Examples: 

Input: arr[] = {2, 1, 2} 
Output: 2 
{2}, {2} and {2, 2} are the subsequences 
with the maximum possible GCD.
Input: arr[] = {1, 2, 3} 
Output: 1 
{3} is the required subsequence. 


Approach: The maximum possible GCD from the array will be equal to the value of the largest element in the array. Now, to maximize the length of the resulting subsequence, find the number of elements with a value equal to this largest value in the array, and the count of these elements is the required answer.
Below is the implementation of the above approach: 
 

C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std;  // Function to return the length // of the largest subsequence with // maximum possible GCD int maxLen(int* arr, int n) {     // Maximum value from the array     int max_val = *max_element(arr, arr + n);      // To store the frequency of the     // maximum element in the array     int freq = 0;      for (int i = 0; i < n; i++) {          // If current element is equal         // to the maximum element         if (arr[i] == max_val)             freq++;     }      return freq; }  // Driver code int main() {     int arr[] = { 3, 2, 2, 3, 3, 3 };     int n = sizeof(arr) / sizeof(int);      cout << maxLen(arr, n);      return 0; } 
Java
// Java implementation of the approach import java.util.Arrays;  class GFG {  // Function to return the length // of the largest subsequence with // maximum possible GCD static int maxLen(int[] arr, int n) {     // Maximum value from the array     int max_val = Arrays.stream(arr).max().getAsInt();      // To store the frequency of the     // maximum element in the array     int freq = 0;      for (int i = 0; i < n; i++)      {          // If current element is equal         // to the maximum element         if (arr[i] == max_val)             freq++;     }     return freq; }  // Driver code public static void main(String []args)  {     int arr[] = { 3, 2, 2, 3, 3, 3 };     int n = arr.length;      System.out.println(maxLen(arr, n)); } }  // This code is contributed by Rajput-Ji 
Python3
# Python3 implementation of the approach  # Function to return the length  # of the largest subsequence with  # maximum possible GCD  def maxLen(arr, n) :          # Maximum value from the array      max_val = max(arr);       # To store the frequency of the      # maximum element in the array      freq = 0;       for i in range(n) :          # If current element is equal          # to the maximum element          if (arr[i] == max_val) :             freq += 1;       return freq;   # Driver code  if __name__ == "__main__" :       arr = [ 3, 2, 2, 3, 3, 3 ];      n = len(arr);       print(maxLen(arr, n));   # This code is contributed by AnkitRai01 
C#
// C# implementation of the approach using System;  using System.Linq;  class GFG  {  // Function to return the length // of the largest subsequence with // maximum possible GCD static int maxLen(int[] arr, int n) {     // Maximum value from the array     int max_val = arr.Max();      // To store the frequency of the     // maximum element in the array     int freq = 0;      for (int i = 0; i < n; i++)      {          // If current element is equal         // to the maximum element         if (arr[i] == max_val)             freq++;     }     return freq; }  // Driver code public static void Main(String []args)  {     int []arr = { 3, 2, 2, 3, 3, 3 };     int n = arr.Length;      Console.WriteLine(maxLen(arr, n)); } }  // This code is contributed by PrinciRaj1992 
JavaScript
<script>  // Javascript implementation of the approach  // Function to return the length // of the largest subsequence with // maximum possible GCD function maxLen(arr, n) {     // Maximum value from the array     var max_val = arr.reduce((a,b) => Math.max(a,b));      // To store the frequency of the     // maximum element in the array     var freq = 0;      for (var i = 0; i < n; i++) {          // If current element is equal         // to the maximum element         if (arr[i] == max_val)             freq++;     }      return freq; }  // Driver code var arr = [3, 2, 2, 3, 3, 3]; var n = arr.length; document.write( maxLen(arr, n));  </script>  

Output: 
4

 

Time Complexity: O(n), where n is the size of the given array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.


Next Article
Longest sub-sequence with maximum GCD

D

DivyanshuShekhar1
Improve
Article Tags :
  • Algorithms
  • DSA
  • Arrays
  • GCD-LCM
  • subsequence
Practice Tags :
  • Algorithms
  • Arrays

Similar Reads

    Longest sub-array with maximum GCD
    Given an array arr[] of positive integers, the task is the find the length of the longest sub-array with the maximum possible GCD value.Examples: Input: arr[] = {1, 2, 2} Output: 2 Here all possible sub-arrays and there GCD's are: 1) {1} -> 1 2) {2} -> 2 3) {2} -> 2 4) {1, 2} -> 1 5) {2,
    9 min read
    Minimum length of subsequence having unit GCD
    Given an array arr[] of N positive integers. The task is to find the length of the shortest sub-sequence such that the GCD of the subsequence is 1. If none of the sub-sequence has GCD 1, then print "-1". Examples: Input: arr[] = {2, 6, 12, 3}Output: 2 Explanation:The GCD of 2, 3 = 1, which is the sm
    15+ min read
    Increasing sequence with given GCD
    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 min read
    Largest subarray with GCD one
    There is an array with n elements. Find length of the largest subarray having GCD equal to 1. If no subarray with GCD 1, then print -1. Examples : Input : 1 3 5 Output : 3 Input : 2 4 6 Output :-1 Recommended PracticeLargest subarray with GCD oneTry It! A simple solution is to consider every subarra
    6 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences