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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Largest subarray having sum greater than k
- K-th Largest Sum Contiguous Subarray
- Largest Sum Contiguous Subarray
- Largest sum subarray with at-least k numbers
- Largest product of a subarray of size k
- Largest sum contiguous increasing subarray
- Largest subarray with equal number of 0s and 1s
- Find the length of largest subarray with 0 sum
- Length of the largest subarray with contiguous elements | Set 1
- Range query for Largest Sum Contiguous Subarray
- Length of largest subarray whose all elements are Perfect Number
- Length of largest subarray whose all elements Powerful number
- Find the length of largest subarray in which all elements are Autobiographical Numbers
- 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