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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Largest subarray having sum greater than k
- K-th Largest Sum Contiguous Subarray
- Largest sum subarray with at-least k numbers
- Largest Sum Contiguous Subarray
- Largest sum contiguous increasing subarray
- Find the length of largest subarray with 0 sum
- Largest product of a subarray of size k
- Largest subarray with equal number of 0s and 1s
- Largest subarray with frequency of all elements same
- Subarray with largest sum after excluding its maximum element
- 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
- Maximum length of subarray such that all elements are equal in the subarray
- 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
- Rearrange an array in order - smallest, largest, 2nd smallest, 2nd largest, ..