Longest sub | C ++ implementation of the approach ; Function to return the length of the largest subarray with maximum possible GCD ; To store the maximum number present in the array ; Finding the maximum element ; To store the final answer ; Two pointer ; Running a loop from j = i ; Condition for incrementing ' j ' ; Updating the answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findLength ( int * arr , int n ) { int x = 0 ; for ( int i = 0 ; i < n ; i ++ ) x = max ( x , arr [ i ] ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != x ) continue ; int j = i ; while ( arr [ j ] == x ) j ++ ; ans = max ( ans , j - i ) ; } return ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 2 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << findLength ( arr , n ) ; return 0 ; }