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 email@example.com. 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.
- 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
- Find the length of largest subarray with 0 sum
- Largest product of a subarray of size k
- Largest Ratio Contiguous subarray
- Largest sum contiguous increasing subarray
- Largest subarray with equal number of 0s and 1s
- Largest subarray with frequency of all elements same
- Range query for Largest Sum Contiguous Subarray
- Length of the largest subarray with contiguous elements | Set 1
- Largest sum contiguous subarray having only non-negative elements
- Subarray with largest sum after excluding its maximum element
- 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