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.
- Largest sum subarray with at-least k numbers
- K-th Largest Sum Contiguous Subarray
- Largest subarray having sum greater than k
- Largest Sum Contiguous Subarray
- Largest product of a subarray of size k
- Find the length of largest subarray with 0 sum
- Largest subarray with equal number of 0s and 1s
- Largest sum contiguous increasing subarray
- Range query for Largest Sum Contiguous Subarray
- Length of the largest subarray with contiguous elements | Set 1
- 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
- Rearrange an array in order - smallest, largest, 2nd smallest, 2nd largest, ..
- Subarray with XOR less than k
- XOR of all subarray XORs | Set 1