codeparrot/xlcost-text-to-code huggingface dataset:
Sum of product of all unordered pairs in given range with update queries | C ++ Program for the above approach ; Vector to store fenwick tree of the 1 st type ; Vector to store fenwick tree of the 2 nd type ; Function to update the value at idx index in fenwick tree ; Function to return the sum of values stored from O to idx index in the array using Fenwick Tree ; Function to build the Fenwick tree from the a [ ] Array ; Function call to update the ith element in the first Fenwick Tree ; Function call to update the ith element in the first Fenwick Tree ; Function to find the Pairwise Product Sum in the range L to R ; Function call to calculate E in the given range ; Function call to calculate E in the given range ; Print Answer ; Function to update the Fenwick tree and the array element at index P to the new value X ; Function call to update the value in 1 st Fenwick Tree ; Function call to update the value in 2 nd Fenwick Tree ; Function to solve Q queries ; Function Call to build the Fenwick Tree ; If Query is of type 1 ; If Query is of type 2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAXN  100000 NEW_LINE vector < int > bit1 ( MAXN , 0 ) ; vector < int > bit2 ( MAXN , 0 ) ; void update ( int idx , int val , vector < int > & bit ) { while ( idx < bit . size ( ) ) { bit [ idx ] += val ; idx += idx & ( - idx ) ; } } int query ( int idx , vector < int > & bit ) { int res = 0 ; while ( idx > 0 ) { res += bit [ idx ] ; idx -= idx & ( - idx ) ; } return res ; } void buildFenwickTree ( int a [ ] , int n ) { for ( int i = 1 ; i <= n ; i ++ ) { update ( i , a [ i - 1 ] , bit1 ) ; update ( i , a [ i - 1 ] * a [ i - 1 ] , bit2 ) ; } } void pairwiseProductSum ( int a [ ] , int l , int r ) { int sum , e , q ; e = query ( r , bit1 ) - query ( l - 1 , bit1 ) ; e = e * e ; q = query ( r , bit2 ) - query ( l - 1 , bit2 ) ; sum = ( e - q ) / 2 ; cout << sum << endl ; } void updateArray ( int * a , int p , int x ) { update ( p , - a [ p - 1 ] , bit1 ) ; update ( p , x , bit1 ) ; update ( p , - a [ p - 1 ] * a [ p - 1 ] , bit2 ) ; update ( p , x * x , bit2 ) ; a [ p - 1 ] = x ; } void solveQueries ( int * a , int n , int Q , int query [ ] [ 3 ] ) { buildFenwickTree ( a , n ) ; for ( int i = 0 ; i < Q ; i ++ ) { if ( query [ i ] [ 0 ] == 1 ) pairwiseProductSum ( a , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; else updateArray ( a , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; } } int main ( ) { int A [ ] = { 5 , 7 , 2 , 3 , 1 } ; int N = sizeof ( A ) / sizeof ( int ) ; int Q = 3 ; int query [ Q ] [ 3 ] = { { 1 , 1 , 3 } , { 2 , 2 , 5 } , { 1 , 2 , 5 } } ; solveQueries ( A , N , Q , query ) ; return 0 ; }
Count of triplets in an Array with odd sum | C ++ Program for the above approach ; Function to count the number of unordered triplets such that their sum is an odd integer ; Count the number of odd and even integers in the array ; Number of ways to create triplets using one odd and two even integers ; Number of ways to create triplets using three odd integers ; Return answer ; Driver Code ; Function Call ; Print Answer
#include <iostream> NEW_LINE using namespace std ; int countTriplets ( int arr [ ] , int n ) { int odd = 0 , even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] & 1 ) odd ++ ; else even ++ ; } int c1 = odd * ( even * ( even - 1 ) ) / 2 ; int c2 = ( odd * ( odd - 1 ) * ( odd - 2 ) ) / 6 ; return c1 + c2 ; } int main ( ) { int arr [ ] = { 4 , 5 , 6 , 4 , 5 , 10 , 1 , 7 } ; int n = sizeof ( arr ) / sizeof ( int ) ; int ans = countTriplets ( arr , n ) ; cout << ans ; return 0 ; }
Longest remaining array of distinct elements possible after repeated removal of maximum and minimum elements of triplets | C ++ Program to implement the above approach ; Function to return length of longest remaining array of pairwise distinct array possible by removing triplets ; Stores the frequency of array elements ; Traverse the array ; Iterate through the map ; If frequency of current element is even ; Stores the required count of unique elements remaining ; If count is odd ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxUniqueElements ( int A [ ] , int N ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < N ; i ++ ) { mp [ A [ i ] ] ++ ; } int cnt = 0 ; for ( auto x : mp ) { if ( x . second % 2 == 0 ) { cnt ++ ; } } int ans = mp . size ( ) ; if ( cnt % 2 == 1 ) { ans -- ; } return ans ; } int main ( ) { int N = 5 ; int A [ ] = { 1 , 2 , 1 , 3 , 7 } ; cout << maxUniqueElements ( A , N ) ; }
Count cells in a grid from which maximum number of cells can be reached by K vertical or horizontal jumps | C ++ program for the above approach ; Function to count the number of cells in the grid such that maximum cell is reachable with a jump of K ; Maximum reachable rows from the current row ; Stores the count of cell that are reachable from the current row ; Count of reachable rows ; Update the maximum value ; Add it to the count ; Maximum reachable columns from the current column ; Stores the count of cell that are reachable from the current column ; Count of rechable columns ; Update the maximum value ; Add it to the count ; Return the total count of cells ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long countCells ( int n , int m , int s ) { int mx1 = -1 ; int cont1 = 0 ; for ( int i = 0 ; i < s && i < n ; ++ i ) { int aux = ( n - ( i + 1 ) ) / s + 1 ; if ( aux > mx1 ) { mx1 = cont1 = aux ; } else if ( aux == mx1 ) cont1 += aux ; } int mx2 = -1 ; int cont2 = 0 ; for ( int i = 0 ; i < s && i < m ; ++ i ) { int aux = ( m - ( i + 1 ) ) / s + 1 ; if ( aux > mx2 ) mx2 = cont2 = aux ; else if ( aux == mx2 ) cont2 += aux ; } return ( long long ) ( cont1 * cont2 ) ; } int main ( ) { int N = 5 , M = 5 , K = 2 ; cout << countCells ( N , M , K ) ; return 0 ; }
Count of pairs from first N natural numbers with remainder at least K | C ++ program for the above approach ; Function to count the number of pairs ( a , b ) such that a % b is at least K ; Base Case ; Stores resultant count of pairs ; Iterate over the range [ K + 1 , N ] ; Find the cycled elements ; Find the remaining elements ; Return the resultant possible count of pairs ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countTotalPairs ( int N , int K ) { if ( K == 0 ) { return N * N ; } int ans = 0 ; for ( int b = K + 1 ; b <= N ; b ++ ) { ans += ( N / b ) * ( b - K ) ; ans += max ( N % b - K + 1 , 0 ) ; } return ans ; } int main ( ) { int N = 5 , K = 2 ; cout << countTotalPairs ( N , K ) ; return 0 ; }
Maximum number of pairs of distinct array elements possible by including each element in only one pair |  ; stores the frequency array ; maxi stores the maximum frequency of an element ; it stores the sum of all the frequency other than the element which has maximum frequency ; there will be always zero or more element which will not participate in making pairs ; if n is odd then except one element we can always form pair for every element if n is even then all the elements can form pair
#include <bits/stdc++.h> NEW_LINE using namespace std ; int main ( ) { int arr [ ] = { 4 , 2 , 4 , 1 , 4 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int maxi = 0 , remain , ans ; map < int , int > freq ; for ( int i = 0 ; i < n ; i ++ ) { freq [ arr [ i ] ] ++ ; } for ( auto it : freq ) { maxi = max ( maxi , it . second ) ; } remain = n - maxi ; if ( maxi >= remain ) { ans = remain ; } else { ans = n / 2 ; } cout << ans ; freq . clear ( ) ; return 0 ; }
Minimize sum of absolute difference between all pairs of array elements by decrementing and incrementing pairs by 1 | C ++ program for the above approach ; Function to find the minimum value of the sum of absolute difference between all pairs of arrays ; Stores the sum of array elements ; Find the sum of array element ; Store the value of sum % N ; Return the resultant value ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minSumDifference ( int ar [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ar [ i ] ; int rem = sum % n ; return rem * ( n - rem ) ; } int main ( ) { int arr [ ] = { 3 , 6 , 8 , 5 , 2 , 1 , 11 , 7 , 10 , 4 } ; int N = sizeof ( arr ) / sizeof ( int ) ; cout << minSumDifference ( arr , N ) ; return 0 ; }
Count N | C ++ program for the above approache ; Function to find the value of x ^ y ; Stores the value of x ^ y ; Iterate until y is positive ; If y is odd ; Divide y by 2 ; Return the value of x ^ y ; Function to find the number of N - digit integers satisfying the given criteria ; Count of even positions ; Count of odd positions ; Return the resultant count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int m = 1000000007 ; int power ( int x , int y ) { int res = 1 ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % m ; y = y >> 1 ; x = ( x * x ) % m ; } return res ; } int countNDigitNumber ( int N ) { int ne = N / 2 + N % 2 ; int no = floor ( N / 2 ) ; return power ( 4 , ne ) * power ( 5 , no ) ; } int main ( ) { int N = 5 ; cout << countNDigitNumber ( N ) % m << endl ; }
Minimum jumps to traverse all integers in range [ 1 , N ] such that integer i can jump i steps | C ++ implementation of the above approach ; Utility function to find minimum steps ; Initialize count and result ; Traverse over the range [ 1 , N ] ; Update res ; Increment count ; Return res ; Driver Code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minSteps ( int N ) { int count = 1 , res = 0 ; for ( int i = 1 ; i <= N ; i += count ) { res = max ( res , count ) ; count ++ ; } return res ; } int main ( ) { int N = 6 ; cout << minSteps ( N ) << " STRNEWLINE " ; }
Minimum jumps to traverse all integers in range [ 1 , N ] such that integer i can jump i steps | C ++ implementation of the above approach ; Utility function to find minimum steps ; Driver code ; Input integer ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minSteps ( int N ) { int res = ( sqrt ( 1 + 8 * N ) - 1 ) / 2 ; return res ; } int main ( ) { int N = 6 ; cout << minSteps ( N ) << " STRNEWLINE " ; }
The dice problem | C ++ program for the above approach ; Function to find number written on the opposite face of the dice ; Stores number on opposite face of dice ; Print the answer ; Driver Code ; Given value of N ; Function call to find number written on the opposite face of the dice
#include <bits/stdc++.h> NEW_LINE using namespace std ; int oppositeFaceOfDice ( int N ) { int ans = 7 - N ; cout << ans ; } int main ( ) { int N = 2 ; oppositeFaceOfDice ( N ) ; return 0 ; }
Count numbers from a given range that can be visited moving any number of steps from the range [ L , R ] | C ++ code for above approach ; Function to count points from the range [ X , Y ] that can be reached by moving by L or R steps ; Initialize difference array ; Initialize Count ; Marking starting point ; Iterating from X to Y ; Accumulate difference array ; If diff_arr [ i ] is greater than 1 ; Updating difference array ; Visited point found ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countReachablePoints ( int X , int Y , int L , int R ) { int diff_arr [ 100000 ] = { 0 } ; int count = 0 ; diff_arr [ X ] = 1 ; diff_arr [ X + 1 ] = -1 ; for ( int i = X ; i <= Y ; i ++ ) { diff_arr [ i ] += diff_arr [ i - 1 ] ; if ( diff_arr [ i ] >= 1 ) { diff_arr [ i + L ] += 1 ; diff_arr [ i + R + 1 ] -= 1 ; count ++ ; } } return count ; } int main ( ) { int X = 3 , Y = 12 , L = 2 , R = 3 ; cout << countReachablePoints ( X , Y , L , R ) ; return 0 ; }
Multiply a single digit number in place to a number represented as a Linked List | C ++ program for the above approach ; Node of a Linked List ; Function to create a new node with given data ; Initialize new node ; Set the data field of the node ; Return the new node ; Function to reverse the linked list ; Traverse until curr != null ; Return the head of the reversed linked list ; Utility function to multiply a single digit to a linked list ; Store the head of list ; Stores the address of previous node ; Initially set carry as 0 and product as 1 ; Multiply M with each digit ; Add carry to product if carry exist ; Update carry for next calculation ; Update the value of each nodes ; Move head and temp pointers to next nodes ; If some carry is still there , add a new node to list ; Return head of the resultant list ; Function to multiply a single digit to a linked list ; Reverse linked list ; Multiply M from left to right of reversed list ; Reverse the modified list and return its head ; Function to print the linked list ; Driver Code ; Given Input list : 1 -> 2 -> 7 -> 3 ; Function Call ; Print resultant list
#include <bits/stdc++.h> NEW_LINE using namespace std ; class Node { public : int data ; Node * next ; } ; Node * newNode ( int data ) { Node * new_node = new Node ; new_node -> data = data ; new_node -> next = NULL ; return new_node ; } Node * reverse ( Node * head ) { Node * prev = NULL ; Node * current = head ; Node * next ; while ( current != NULL ) { next = current -> next ; current -> next = prev ; prev = current ; current = next ; } return prev ; } Node * multiplyHelp ( Node * head , int M ) { Node * res = head ; Node * prev = NULL ; int carry = 0 , product = 1 ; while ( head != NULL ) { product = head -> data * M ; product += carry ; carry = product / 10 ; head -> data = product % 10 ; prev = head ; head = head -> next ; } if ( carry > 0 ) prev -> next = newNode ( carry ) ; return res ; } Node * multiply ( Node * head , int M ) { head = reverse ( head ) ; head = multiplyHelp ( head , M ) ; return reverse ( head ) ; } void printList ( Node * node ) { while ( node != NULL ) { cout << node -> data ; node = node -> next ; } } int main ( ) { Node * head = newNode ( 1 ) ; head -> next = newNode ( 2 ) ; head -> next -> next = newNode ( 7 ) ; head -> next -> next -> next = newNode ( 3 ) ; int M = 3 ; head = multiply ( head , M ) ; printList ( head ) ; return 0 ; }
Count of triplets having sum of product of any two numbers with the third number equal to N | C ++ program for the above approach ; Function to find the SPF [ i ] using the Sieve Of Erastothenes ; Stores whether i is prime or not ; Initializing smallest factor as 2 for all even numbers ; Iterate for all odd numbers < N ; SPF of i for a prime is the number itself ; Iterate for all the multiples of the current prime number ; The value i is smallest prime factor for i * j ; Function to generate prime factors and its power ; Current prime factor of N ; Stores the powers of the current prime factor ; Find all the prime factors and their powers ; Return the total count of factors ; Function to count the number of triplets satisfying the given criteria ; Stores the count of resultant triplets ; Add the count all factors of N - z to the variable CountTriplet ; Return total count of triplets ; Driver Code ; Find the SPF [ i ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > s ( 11 , 0 ) ; void sieveOfEratosthenes ( int N ) { bool prime [ N + 1 ] ; memset ( prime , false , sizeof ( false ) ) ; for ( int i = 2 ; i < N + 1 ; i += 2 ) s [ i ] = 2 ; for ( int i = 3 ; i < N + 1 ; i += 2 ) { if ( prime [ i ] == false ) { s [ i ] = i ; for ( int j = i ; j < N / i + 1 ; j += 2 ) { if ( prime [ i * j ] == false ) { prime [ i * j ] = true ; s [ i * j ] = i ; } } } } } int generatePrimeFactors ( int N ) { int curr = s [ N ] ; map < int , int > cnt ; cnt [ s [ N ] ] = 1 ; while ( N > 1 ) { N /= s [ N ] ; if ( N and s [ N ] ) if ( cnt . find ( s [ N ] ) == cnt . end ( ) ) cnt [ s [ N ] ] = 1 ; else cnt [ s [ N ] ] += 1 ; } if ( cnt . find ( 0 ) != cnt . end ( ) ) cnt . erase ( 0 ) ; int totfactor = 1 ; for ( auto i : cnt ) totfactor *= i . second + 1 ; return totfactor ; } int countTriplets ( int N ) { int CountTriplet = 0 ; for ( int z = 1 ; z < N + 1 ; z ++ ) { int p = generatePrimeFactors ( N - z ) ; if ( p > 1 ) CountTriplet += p ; } return CountTriplet + 1 ; } int main ( ) { int N = 10 ; sieveOfEratosthenes ( N ) ; cout << countTriplets ( N ) ; }
Minimum length of the subarray required to be replaced to make frequency of array elements equal to N / M | C ++ program for the above approach ; Function to find the minimum length of the subarray to be changed . ; Stores the frequencies of array elements ; Stores the number of array elements that are present more than N / M times ; Iterate over the range ; Increment the frequency ; If the frequency of all array elements are already N / M ; Stores the resultant length of the subarray ; The left and right pointers ; Iterate over the range ; If the current element is ; If the value of c is 0 , then find the possible answer ; Iterate over the range ; If the element at left is making it extra ; Update the left pointer ; Update the right pointer ; Return the resultant length ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumSubarray ( vector < int > arr , int n , int m ) { vector < int > mapu ( m + 1 , 0 ) ; int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mapu [ arr [ i ] ] ++ ; if ( mapu [ arr [ i ] ] == ( n / m ) + 1 ) c ++ ; } if ( c == 0 ) return 0 ; int ans = n ; int l = 0 , r = 0 ; while ( r < n ) { if ( -- mapu [ arr [ r ] ] == ( n / m ) ) c -- ; if ( c == 0 ) { while ( l <= r && c == 0 ) { ans = min ( ans , r - l + 1 ) ; if ( ++ mapu [ arr [ l ] ] > ( n / m ) ) c ++ ; l ++ ; } } r ++ ; } return ans ; } int main ( ) { vector < int > arr = { 1 , 1 , 2 , 1 , 1 , 2 } ; int M = 2 ; int N = arr . size ( ) ; cout << minimumSubarray ( arr , N , M ) ; return 0 ; }
Check if the sum of K least and most frequent array elements are equal or not | C ++ program for the above approach ; Function to compare the sum of K most and least occurrences ; Stores frequency of array element ; Stores the frequencies as indexes and putelements with the frequency in a vector ; Find the frequency ; Insert in the vector ; Stores the count of elements ; Traverse the frequency array ; Find the kleastfreqsum ; If the count is K , break ; Reinitialize the count to zero ; Traverse the frequency ; Find the kmostfreqsum ; If the count is K , break ; Comparing the sum ; Otherwise , return No ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string checkSum ( int arr [ ] , int n , int k ) { unordered_map < int , int > m ; for ( int i = 0 ; i < n ; i ++ ) m [ arr [ i ] ] ++ ; vector < int > freq [ n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { int f = m [ arr [ i ] ] ; if ( f != -1 ) { freq [ f ] . push_back ( arr [ i ] ) ; m [ arr [ i ] ] = -1 ; } } int count = 0 ; int kleastfreqsum = 0 ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int x : freq [ i ] ) { kleastfreqsum += x ; count ++ ; if ( count == k ) break ; } if ( count == k ) break ; } count = 0 ; int kmostfreqsum = 0 ; for ( int i = n ; i >= 0 ; i -- ) { for ( int x : freq [ i ] ) { kmostfreqsum += x ; count ++ ; if ( count == k ) break ; } if ( count == k ) break ; } if ( kleastfreqsum == kmostfreqsum ) return " Yes " ; return " No " ; } int main ( ) { int arr [ ] = { 3 , 2 , 1 , 2 , 3 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 2 ; cout << checkSum ( arr , N , K ) ; return 0 ; }
Print all numbers from a given range that are made up of consecutive digits | C ++ program for the above approach ; Function to find the consecutive digit numbers in the given range ; Initialize num as empty string ; Stores the resultant number ; Iterate over the range [ 1 , 9 ] ; Check if the current number is within range ; Iterate on the digits starting from i ; Checking the consecutive digits numbers starting from i and ending at j is within range or not ; Sort the numbers in the increasing order ; Driver Code ; Print the required numbers
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > solve ( int start , int end ) { string num = " " ; vector < int > ans ; for ( int i = 1 ; i <= 9 ; i ++ ) { num = to_string ( i ) ; int value = stoi ( num ) ; if ( value >= start and value <= end ) { ans . push_back ( value ) ; } for ( int j = i + 1 ; j <= 9 ; j ++ ) { num += to_string ( j ) ; value = stoi ( num ) ; if ( value >= start and value <= end ) { ans . push_back ( value ) ; } } } sort ( ans . begin ( ) , ans . end ( ) ) ; return ans ; } int main ( ) { int L = 12 , R = 87 ; vector < int > ans = solve ( 12 , 87 ) ; for ( auto & it : ans ) cout << it << ' ▁ ' ; return 0 ; }
Minimum distance a person has to move in order to take a picture of every racer | C ++ implementation of the approach ; Function to return the minimum distance the person has to move int order to get the pictures ; To store the minimum ending point ; To store the maximum starting point ; Find the values of minSeg and maxSeg ; Impossible ; The person doesn 't need to move ; Get closer to the left point ; Get closer to the right point ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minDistance ( int start [ ] , int end [ ] , int n , int d ) { int left = INT_MIN ; int right = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { left = max ( left , start [ i ] ) ; right = min ( right , end [ i ] ) ; } if ( left > right ) return -1 ; if ( d >= left && d <= right ) return 0 ; if ( d < left ) return ( left - d ) ; if ( d > right ) return ( d - right ) ; } int main ( ) { int start [ ] = { 0 , 2 , 4 } ; int end [ ] = { 7 , 14 , 6 } ; int n = sizeof ( start ) / sizeof ( int ) ; int d = 3 ; cout << minDistance ( start , end , n , d ) ; return 0 ; }
Maximum number of Armstrong Numbers present in a subarray of size K | C ++ program for the above approach ; Function to calculate the value of x raised to the power y in O ( log y ) ; Base Case ; If the power y is even ; Otherwise ; Function to calculate the order of the number , i . e . count of digits ; Stores the total count of digits ; Iterate until num is 0 ; Function to check a number is an Armstrong Number or not ; Find the order of the number ; Check for Armstrong Number ; If Armstrong number condition is satisfied ; Utility function to find the maximum sum of a subarray of size K ; If k is greater than N ; Find the sum of first subarray of size K ; Find the sum of the remaining subarray ; Return the maximum sum of subarray of size K ; Function to find all the Armstrong Numbers in the array ; Traverse the array arr [ ] ; If arr [ i ] is an Armstrong Number , then replace it by 1. Otherwise , 0 ; Return the resultant count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int power ( int x , unsigned int y ) { if ( y == 0 ) return 1 ; if ( y % 2 == 0 ) return power ( x , y / 2 ) * power ( x , y / 2 ) ; return x * power ( x , y / 2 ) * power ( x , y / 2 ) ; } int order ( int num ) { int count = 0 ; while ( num ) { count ++ ; num = num / 10 ; } return count ; } int isArmstrong ( int N ) { int r = order ( N ) ; int temp = N , sum = 0 ; while ( temp ) { int d = temp % 10 ; sum += power ( d , r ) ; temp = temp / 10 ; } return ( sum == N ) ; } int maxSum ( int arr [ ] , int N , int K ) { if ( N < K ) { return -1 ; } int res = 0 ; for ( int i = 0 ; i < K ; i ++ ) { res += arr [ i ] ; } int curr_sum = res ; for ( int i = K ; i < N ; i ++ ) { curr_sum += arr [ i ] - arr [ i - K ] ; res = max ( res , curr_sum ) ; } return res ; } int maxArmstrong ( int arr [ ] , int N , int K ) { for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] = isArmstrong ( arr [ i ] ) ; } return maxSum ( arr , N , K ) ; } int main ( ) { int arr [ ] = { 28 , 2 , 3 , 6 , 153 , 99 , 828 , 24 } ; int K = 6 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxArmstrong ( arr , N , K ) ; return 0 ; }