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.
Input : 1 3 5 Output : 3 Input : 2 4 6 Output :-1
A simple solution is to consider every subarray and find its GCD and keep track of largest subarray with GCD one. Finally return length of the largest subarray with GCD 1.
An efficient solution is based on fact that if any two elements have GCD equals to one, then whole array has GCD one. So the output is either -1 or length of array.
Length of the largest subarray = 4
This article is contributed by Smarak Chopdar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Smallest subsequence having GCD equal to GCD of given array
- Smallest Subarray with given GCD
- Smallest subarray with GCD as 1 | Segment Tree
- Length of the Smallest Subarray that must be removed in order to Maximise the GCD
- Length of longest subarray of length at least 2 with maximum GCD
- Python program to Find the Jumbo GCD subarray
- Largest subsequence having GCD greater than 1
- Largest Subset with GCD 1
- Series with largest GCD and sum equals to n
- Lexicographically largest permutation of the array such that a[i] = a[i-1] + gcd(a[i-1], a[i-2])
- GCD of two numbers when one of them can be very large
- GCD of array is greater than one
- Minimum gcd operations to make all array elements one
- Pairs with GCD equal to one in the given range
- Maximum possible GCD after replacing at most one element in the given array
- Maximize the subarray sum after multiplying all elements of any subarray with X
- Maximum length of subarray such that sum of the subarray is even
- Count of subarray that does not contain any subarray with sum 0
- Maximum length of subarray such that all elements are equal in the subarray