codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of colors required to color a Circular Array | C ++ implementation of above approach ; Function that finds minimum number of colors required ; To check that if all the elements are same or not ; To check if only one adjacent exist ; Traverse the array ; If adjacent elements found different means all are not same ; If two adjacent elements found to be same then make one_adjacent_same true ; If all elements are same then print 1 ; If total number of elements are even or there exist two adjacent elements that are same then print 2 ; Else 3 type of colors are required ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void colorRequired ( int arr [ ] , int n ) { bool all_same = true ; bool one_adjacent_same = false ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] != arr [ i + 1 ] ) { all_same = false ; } if ( arr [ i ] == arr [ i + 1 ] ) { one_adjacent_same = true ; } } if ( all_same == true ) { cout << 1 << endl ; return ; } if ( n % 2 == 0 one_adjacent_same == true ) { cout << 2 << endl ; return ; } cout << 3 << endl ; } int main ( ) { int arr [ ] = { 1 , 2 , 1 , 1 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; colorRequired ( arr , n ) ; return 0 ; }
Maximize the sum of sum of the Array by removing end elements | C ++ program to maximize the sum of sum of the Array by removing end elements ; Function to find the maximum sum of sum ; compute the sum of whole array ; Traverse and remove the minimum value from an end to maximum the sum value ; If the left end element is smaller than right end ; remove the left end element ; If the right end element is smaller than left end ; remove the right end element ; Add the remaining element sum in the result ; Return the maximum sum of sum ; Driver code
#include <iostream> NEW_LINE using namespace std ; int maxRemainingSum ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; int i = 0 ; int j = n - 1 ; int result = 0 ; while ( i < j ) { if ( arr [ i ] < arr [ j ] ) { sum -= arr [ i ] ; i ++ ; } else { sum -= arr [ j ] ; j -- ; } result += sum ; } return result ; } int main ( ) { int arr [ ] = { 3 , 1 , 7 , 2 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxRemainingSum ( arr , N ) ; return 0 ; }
Split a number as sum of K numbers which are not divisible by K | C ++ program to split a number as sum of K numbers which are not divisible by K ; Function to split into K parts and print them ; Print 1 K - 1 times ; Print N - K + 1 ; Print 1 K - 2 times ; Print 2 and N - K ; Driver code
#include <iostream> NEW_LINE using namespace std ; void printKParts ( int N , int K ) { if ( N % K == 0 ) { for ( int i = 1 ; i < K ; i ++ ) cout << "1 , ▁ " ; cout << N - ( K - 1 ) << endl ; } else { if ( K == 2 ) { cout << " Not ▁ Possible " << endl ; return ; } for ( int i = 1 ; i < K - 1 ; i ++ ) cout << 1 << " , ▁ " ; cout << 2 << " , ▁ " << N - K << endl ; } } int main ( ) { int N = 18 , K = 5 ; printKParts ( N , K ) ; return 0 ; }
Make the intervals non | C ++ implementation for intervals scheduling to two processors such that there are no overlapping intervals ; Function to assign the intervals to two different processors ; Loop to pair the interval with their indices ; sorting the interval by their start times ; Loop to iterate over the intervals with their start time ; Condition to check if there is a possible solution ; form = ''.join(form) ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void assignIntervals ( vector < vector < int > > interval , int n ) { for ( int i = 0 ; i < n ; i ++ ) interval [ i ] . push_back ( i ) ; sort ( interval . begin ( ) , interval . end ( ) ) ; int firstEndTime = -1 ; int secondEndTime = -1 ; char fin = ' ▁ ' ; bool flag = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( interval [ i ] [ 0 ] >= firstEndTime ) { firstEndTime = interval [ i ] [ 1 ] ; interval [ i ] . push_back ( ' S ' ) ; } else if ( interval [ i ] [ 0 ] >= secondEndTime ) { secondEndTime = interval [ i ] [ 1 ] ; interval [ i ] . push_back ( ' F ' ) ; } else { flag = true ; break ; } } if ( flag ) cout << ( -1 ) ; else { vector < char > form ( n , ' ▁ ' ) ; for ( int i = 0 ; i < n ; i ++ ) { int indi = interval [ i ] [ 2 ] ; form [ indi ] = interval [ i ] [ 3 ] ; } for ( int i = 0 ; i < form . size ( ) ; i ++ ) cout << form [ i ] << " , " ; } } int main ( ) { vector < vector < int > > intervals = { { 360 , 480 } , { 420 , 540 } , { 600 , 660 } } ; assignIntervals ( intervals , intervals . size ( ) ) ; return 0 ; }
Minimum operation required to make a balanced sequence | C ++ implementation of the approach ; Function to return the minimum operations required ; Condition where we got only one closing bracket instead of 2 , here we have to add one more closing bracket to make the sequence balanced ; Add closing bracket that costs us one operation ; Remove the top opening bracket because we got the 1 opening and 2 continuous closing brackets ; Inserting the opening bracket to stack ; After making the sequence balanced closing is now set to 0 ; Case when there is no opening bracket the sequence starts with a closing bracket and one opening bracket is required Now we have one opening and one closing bracket ; Add opening bracket that costs us one operation ; Assigning 1 to cntClosing because we have one closing bracket ; Case where we got two continuous closing brackets Need to pop one opening bracket from stack top ; Condition where stack is not empty This is the case where we have only opening brackets ( st . size ( ) * 2 ) will give us the total closing bracket needed cntClosing is the count of closing bracket that we already have ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperations ( string s , int len ) { int operationCnt = 0 ; stack < char > st ; int cntClosing = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( s [ i ] == ' { ' ) { if ( cntClosing > 0 ) { operationCnt ++ ; st . pop ( ) ; } st . push ( s [ i ] ) ; cntClosing = 0 ; } else if ( st . empty ( ) ) { st . push ( ' { ' ) ; operationCnt ++ ; cntClosing = 1 ; } else { cntClosing = ( cntClosing + 1 ) % 2 ; if ( cntClosing == 0 ) { st . pop ( ) ; } } } operationCnt += st . size ( ) * 2 - cntClosing ; return operationCnt ; } int main ( ) { string str = " } } { " ; int len = str . length ( ) ; cout << minOperations ( str , len ) ; return 0 ; }
Longest subsequence with different adjacent characters | C ++ program for the above approach ; Function to find the longest Subsequence with different adjacent character ; Length of the string s ; Previously picked character ; If the current character is different from the previous then include this character and update previous character ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestSubsequence ( string s ) { int n = s . length ( ) ; int answer = 0 ; char prev = ' - ' ; for ( int i = 0 ; i < n ; i ++ ) { if ( prev != s [ i ] ) { prev = s [ i ] ; answer ++ ; } } return answer ; } int main ( ) { string str = " ababa " ; cout << longestSubsequence ( str ) ; return 0 ; }
Length of longest subarray of length at least 2 with maximum GCD | C ++ program for the above approach ; Function to calculate GCD of two numbers ; Function to find maximum size subarray having maximum GCD ; Base Case ; Let the maximum GCD be 1 initially ; Loop thourgh array to find maximum GCD of subarray with size 2 ; Traverse the array ; Is a multiple of k , increase cnt ; Else update maximum length with consecutive element divisible by k Set cnt to 0 ; Update the maxLength ; Return the maxLength ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } int maximumGcdSubarray ( int arr [ ] , int n ) { if ( n == 1 ) return 0 ; int k = 1 ; for ( int i = 1 ; i < n ; ++ i ) { k = max ( k , gcd ( arr [ i ] , arr [ i - 1 ] ) ) ; } int cnt = 0 ; int maxLength = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) { cnt ++ ; } else { maxLength = max ( maxLength , cnt ) ; cnt = 0 ; } } maxLength = max ( maxLength , cnt ) ; return maxLength ; } int main ( ) { int arr [ ] = { 18 , 3 , 6 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maximumGcdSubarray ( arr , n ) ; return 0 ; }
Split N powers of 2 into two subsets such that their difference of sum is minimum | C ++ program to find the minimum difference possible by splitting all powers of 2 up to N into two sets of equal size ; Store the largest ; Form two separate groups ; Initialize the sum for two groups as 0 ; Insert 2 ^ n in the first group ; Calculate the sum of least n / 2 - 1 elements added to the first set ; Sum of remaining n / 2 - 1 elements ; Min Difference between the two groups ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void MinDiff ( int n ) { int val = pow ( 2 , n ) ; int sep = n / 2 ; int grp1 = 0 ; int grp2 = 0 ; grp1 = grp1 + val ; for ( int i = 1 ; i < sep ; i ++ ) grp1 = grp1 + pow ( 2 , i ) ; for ( int i = sep ; i < n ; i ++ ) grp2 = grp2 + pow ( 2 , i ) ; cout << ( abs ( grp1 - grp2 ) ) ; } int main ( ) { int n = 4 ; MinDiff ( n ) ; }
Count elements in first Array with absolute difference greater than K with an element in second Array | C ++ program to count elements in first Array with absolute difference greater than K with an element in second Array ; Function to count the such elements ; Store count of required elements in arr1 ; Initialise the smallest and the largest value from the second array arr2 [ ] ; Find the smallest and the largest element in arr2 ; Check if absolute difference of smallest and arr1 [ i ] or largest and arr1 [ i ] is > K then arr [ i ] is a required element ; Print the final result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countDist ( int arr1 [ ] , int n , int arr2 [ ] , int m , int k ) { int count = 0 ; int smallest = arr2 [ 0 ] ; int largest = arr2 [ 0 ] ; for ( int i = 0 ; i < m ; i ++ ) { smallest = max ( smallest , arr2 [ i ] ) ; largest = min ( largest , arr1 [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { if ( abs ( arr1 [ i ] - smallest ) > k || abs ( arr1 [ i ] - largest ) > k ) count ++ ; } cout << count ; } int main ( ) { int arr1 [ ] = { 3 , 1 , 4 } ; int n = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; int arr2 [ ] = { 5 , 1 , 2 } ; int m = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; int k = 2 ; countDist ( arr1 , n , arr2 , m , k ) ; return 0 ; }
Make sum of all subarrays of length K equal by only inserting elements | C ++ implementation of above approach ; Function that prints another array whose all subarray of length k have an equal sum ; Store all distinct elements in the unordered map ; Condition if the number of distinct elements is greater than k ; Push all distinct elements in a vector ; Push 1 while the size of vector not equal to k ; Print the vector 2 times ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void MakeArray ( int a [ ] , int n , int k ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . find ( a [ i ] ) == mp . end ( ) ) mp [ a [ i ] ] = 1 ; } if ( mp . size ( ) > k ) { cout << " Not ▁ possible STRNEWLINE " ; return ; } vector < int > ans ; for ( auto i : mp ) { ans . push_back ( i . first ) ; } while ( ans . size ( ) < k ) { ans . push_back ( 1 ) ; } for ( int i = 0 ; i < 2 ; i ++ ) { for ( int j = 0 ; j < k ; j ++ ) cout << ans [ j ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 1 , 2 , 2 , 1 } ; int K = 2 ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; MakeArray ( arr , size , K ) ; return 0 ; }
Longest substring with atmost K characters from the given set of characters | C ++ implementation to find the longest substring in the string which contains atmost K characters from the given set of characters ; Function to find the longest substring in the string which contains atmost K characters from the given set of characters ; Base Condition ; Count for Characters from set in substring ; Two pointers ; Loop to iterate until right pointer is not equal to N ; Loop to increase the substring length until the characters from set are at most K ; Check if current pointer points a character from set ; If the count of the char is exceeding the limit ; update answer with substring length ; Increment the left pointer until the count is less than or equal to K ; If the character which comes out is normal character then decrement the count by 1 ; Driver Code ; Construction of set ; output result
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxNormalSubstring ( string & P , set < char > Q , int K , int N ) { if ( K == 0 ) return 0 ; int count = 0 ; int left = 0 , right = 0 ; int ans = 0 ; while ( right < N ) { while ( right < N && count <= K ) { if ( Q . find ( P [ right ] ) != Q . end ( ) ) { if ( count + 1 > K ) break ; else count ++ ; } right ++ ; if ( count <= K ) ans = max ( ans , right - left ) ; } while ( left < right ) { left ++ ; if ( Q . find ( P [ left - 1 ] ) != Q . end ( ) ) count -- ; if ( count < K ) break ; } } return ans ; } int main ( ) { string P = " giraffe " ; set < char > Q ; Q . insert ( ' a ' ) ; Q . insert ( ' f ' ) ; Q . insert ( ' g ' ) ; Q . insert ( ' r ' ) ; int K = 2 ; int N = P . length ( ) ; cout << maxNormalSubstring ( P , Q , K , N ) ; return 0 ; }
Find the minimum value of the given expression over all pairs of the array | C ++ program to find the minimum value of the given expression over all pairs of the array ; Function to find the minimum value of the expression ; Iterate over all the pairs and find the minimum value ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MinimumValue ( int a [ ] , int n ) { int answer = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { answer = min ( answer , ( ( a [ i ] & a [ j ] ) ^ ( a [ i ] a [ j ] ) ) ) ; } } return answer ; } int main ( ) { int N = 6 ; int A [ N ] = { 12 , 3 , 14 , 5 , 9 , 8 } ; cout << MinimumValue ( A , N ) ; return 0 ; }
Number of substrings with length divisible by the number of 1 's in it | C ++ program to count number of substring under given condition ; Function return count of such substring ; Mark 1 at those indices where '1' appears ; Take prefix sum ; Iterate through all the substrings ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOfSubstrings ( string s ) { int n = s . length ( ) ; int prefix_sum [ n ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) prefix_sum [ i ] = 1 ; } for ( int i = 1 ; i < n ; i ++ ) prefix_sum [ i ] += prefix_sum [ i - 1 ] ; int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int countOfOnes = prefix_sum [ j ] - ( i - 1 >= 0 ? prefix_sum [ i - 1 ] : 0 ) ; int length = j - i + 1 ; if ( countOfOnes > 0 && length % countOfOnes == 0 ) answer ++ ; } } return answer ; } int main ( ) { string S = "1111100000" ; cout << countOfSubstrings ( S ) ; return 0 ; }
Find the number of elements X such that X + K also exists in the array | C ++ implementation of find number of elements x in this array such x + k also present in this array . ; Function to return the count of element x such that x + k also lies in this array ; Key in map will store elements and value will store the frequency of the elements ; Find if i . first + K is present in this map or not ; If we find i . first or key + K in this map then we have to increase in answer the frequency of this element ; Driver code ; array initialisation ; size of array ; initialise k
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count_element ( int N , int K , int * arr ) { map < int , int > mp ; for ( int i = 0 ; i < N ; ++ i ) mp [ arr [ i ] ] ++ ; int answer = 0 ; for ( auto i : mp ) { if ( mp . find ( i . first + K ) != mp . end ( ) ) answer += i . second ; } return answer ; } int main ( ) { int arr [ ] = { 3 , 6 , 2 , 8 , 7 , 6 , 5 , 9 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 2 ; cout << count_element ( N , K , arr ) ; return 0 ; }
Find the size of largest group where groups are according to the xor of digits | c ++ implementation to Find the size of largest group , where groups are according to the xor of its digits . ; Function to find out xor of digit ; calculate xor digitwise ; return xor ; Function to find the size of largest group ; hash map for counting frequency ; counting freq of each element ; find the maximum ; Driver code ; initialise N
#include <bits/stdc++.h> NEW_LINE using namespace std ; int digit_xor ( int x ) { int xorr = 0 ; while ( x ) { xorr ^= x % 10 ; x = x / 10 ; } return xorr ; } int find_count ( int n ) { map < int , int > mpp ; for ( int i = 1 ; i <= n ; i ++ ) { mpp [ digit_xor ( i ) ] += 1 ; } int maxm = 0 ; for ( auto x : mpp ) { if ( x . second > maxm ) maxm = x . second ; } return maxm ; } int main ( ) { int N = 13 ; cout << find_count ( N ) ; return 0 ; }
Count of largest sized groups while grouping according to product of digits | C ++ implementation to Count the groups having largest size while grouping is according to the product of its digits ; Function to find out product of digit ; calculate product ; return the product of digits ; Function to find the count ; hash map for counting frequency ; counting freq of each element ; find the maximum ; count the number of groups having size of equal to largest group . ; Driver code ; initialise N
#include <bits/stdc++.h> NEW_LINE using namespace std ; int digit_prod ( int x ) { int prod = 1 ; while ( x ) { prod *= x % 10 ; x = x / 10 ; } return prod ; } int find_count ( int n ) { map < int , int > mpp ; for ( int i = 1 ; i <= n ; i ++ ) { mpp [ digit_prod ( i ) ] += 1 ; } int ans = 1 ; int maxm = 0 ; for ( auto x : mpp ) { if ( x . second > maxm ) { maxm = x . second ; ans = 1 ; } else if ( x . second == maxm ) { ans ++ ; } } return ans ; } int main ( ) { int N = 13 ; cout << find_count ( N ) ; return 0 ; }
Count of subarrays having exactly K prime numbers | C ++ program for the above approach ; A utility function to check if the number n is prime or not ; Base Cases ; Check to skip middle five numbers in below loop ; If n is divisible by i & i + 2 then it is not prime ; Function to find number of subarrays with sum exactly equal to k ; STL map to store number of subarrays starting from index zero having particular value of sum . ; To store the sum of element traverse so far ; Add current element to currsum ; If currsum = K , then a new subarray is found ; If currsum > K then find the no . of subarrays with sum currsum - K and exclude those subarrays ; Add currsum to count of different values of sum ; Return the final result ; Function to count the subarray with K primes ; Update the array element ; If current element is prime then update the arr [ i ] to 1 ; Else change arr [ i ] to 0 ; Function Call ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int n ) { int i ; if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) { return false ; } for ( i = 5 ; i * i <= n ; i += 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } int findSubarraySum ( int arr [ ] , int n , int K ) { unordered_map < int , int > prevSum ; int res = 0 ; int currsum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { currsum += arr [ i ] ; if ( currsum == K ) { res ++ ; } if ( prevSum . find ( currsum - K ) != prevSum . end ( ) ) res += ( prevSum [ currsum - K ] ) ; prevSum [ currsum ] ++ ; } return res ; } void countSubarray ( int arr [ ] , int n , int K ) { for ( int i = 0 ; i < n ; i ++ ) { if ( isPrime ( arr [ i ] ) ) { arr [ i ] = 1 ; } else { arr [ i ] = 0 ; } } cout << findSubarraySum ( arr , n , K ) ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int K = 2 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countSubarray ( arr , N , K ) ; return 0 ; }
Count the elements having frequency equals to its value | C ++ program to count the elements having frequency equals to its value ; Function to find the count ; Hash map for counting frequency ; Counting freq of each element ; Check if value equals to frequency and increment the count ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int find_maxm ( int arr [ ] , int n ) { map < int , int > mpp ; for ( int i = 0 ; i < n ; i ++ ) { mpp [ arr [ i ] ] += 1 ; } int ans = 0 ; for ( auto x : mpp ) { int value = x . first ; int freq = x . second ; if ( value == freq ) { ans ++ ; } } return ans ; } int main ( ) { int arr [ ] = { 3 , 2 , 2 , 3 , 4 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << find_maxm ( arr , n ) ; return 0 ; }
Find the Number of Permutations that satisfy the given condition in an array | C ++ program to find the number of permutations that satisfy the given condition in an array ; Function to calculate x ^ y recursively ; Function to return the number of permutations that satisfy the given condition in an array ; If there is only one element then only one permutation is available ; Sort the array for calculating the number of elements occurring twice ; If the maximum element is occurring twice , then the number of permutations satisfying the condition is 0 ; This variable will store the number of element occurring twice ; Loop to check the number of elements occurring twice ; Check if this element is occurring twice ; If this element is occurring twice then check if this number is occurring more than twice ; If element occurring thrice then no permutation will satisfy the given condition ; Since we have checked the next element as well , then we can increment the loop variable ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int pow ( int x , int y ) { if ( y == 1 ) return x ; if ( y == 0 ) return 1 ; int temp = pow ( x , y / 2 ) ; temp *= temp ; if ( y & 1 ) temp *= x ; return temp ; } int noOfPermutations ( int * a , int n ) { if ( n == 1 ) { return 1 ; } sort ( a , a + n ) ; if ( a [ n - 1 ] == a [ n - 2 ] ) { return 0 ; } int x = 0 ; for ( int i = 0 ; i < n - 2 ; ++ i ) { if ( a [ i ] == a [ i + 1 ] ) { if ( a [ i ] == a [ i + 2 ] ) { return 0 ; } x ++ ; i ++ ; } } return pow ( 2 , n - 2 * x - 1 ) ; } int main ( ) { int a [ ] = { 1 , 2 , 2 , 3 , 4 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; int num = noOfPermutations ( a , n ) ; cout << num ; return 0 ; }
Length of the Smallest Subarray that must be removed in order to Maximise the GCD | C ++ program to find the length of the smallest subarray that must be removed in order to maximise the GCD ; Function to find the length of the smallest subarray that must be removed in order to maximise the GCD ; Store the maximum possible GCD of the resulting subarray ; Two pointers initially pointing to the first and last element respectively ; Moving the left pointer to the right if the elements are divisible by the maximum GCD ; Moving the right pointer to the left if the elements are divisible by the maximum GCD ; Return the length of the subarray ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int GetMinSubarrayLength ( int a [ ] , int n ) { int ans = max ( a [ 0 ] , a [ n - 1 ] ) ; int lo = 0 , hi = n - 1 ; while ( lo < n and a [ lo ] % ans == 0 ) lo ++ ; while ( hi > lo and a [ hi ] % ans == 0 ) hi -- ; return ( hi - lo + 1 ) ; } int main ( ) { int arr [ ] = { 4 , 8 , 2 , 1 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int length = GetMinSubarrayLength ( arr , N ) ; cout << length << " STRNEWLINE " ; return 0 ; }