codeparrot/xlcost-text-to-code huggingface dataset:
Check if an array can be converted to another given array by swapping pairs of unequal elements | C ++ program for the above approach ; Function to check if arr1 [ ] can be converted to arr2 [ ] by swapping pair ( i , j ) such that i < j and arr [ i ] is 1 and arr [ j ] is 0 ; Stores the differences of prefix sum of arr1 and arr2 ; Stores the count of 1 and zero of arr1 ; Stores the count of 1 and zero of arr2 ; Iterate in the range [ 0 , N - 1 ] ; If arr1 [ i ] is 1 , then increment arr1_one by one ; Otherwise increment arr1_zero by one ; If arr2 [ i ] is 1 , then increment arr2_one by one ; Otherwise increment arr2_zero by one ; Check if number of 1 s and 0 s of arr1 is equal to number of 1 s and 0 s of arr2 respectievly ; Iterate over the range [ 0 , N - 1 ] ; Increment count by differences arr1 [ i ] and arr2 [ i ] ; Check if number of 1 's in  arr2 are more than arr1 and  then print "No" ; Finally , print " Yes " ; Driver Code ; Given input arrays ; Size of the array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void canMakeEqual ( int arr1 [ ] , int arr2 [ ] , int N ) { int count = 0 ; int arr1_one = 0 , arr1_zero = 0 ; int arr2_one = 0 , arr2_zero = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr1 [ i ] == 1 ) { arr1_one ++ ; } else if ( arr1 [ i ] == 0 ) { arr1_zero ++ ; } if ( arr2 [ i ] == 1 ) { arr2_one ++ ; } else if ( arr2 [ i ] == 0 ) { arr2_zero ++ ; } } if ( arr1_one != arr2_one arr1_zero != arr2_zero ) { cout << " No " ; return ; } for ( int i = 0 ; i < N ; i ++ ) { count = count + ( arr1 [ i ] - arr2 [ i ] ) ; if ( count < 0 ) { cout << " No " ; return ; } } cout << " Yes " ; } int main ( ) { int arr1 [ ] = { 0 , 1 , 1 , 0 } ; int arr2 [ ] = { 0 , 0 , 1 , 1 } ; int N = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; canMakeEqual ( arr1 , arr2 , N ) ; return 0 ; }
Check for each subarray whether it consists of all natural numbers up to its length or not | C ++ program to implement the above approach ; Function to check if a subarray of size i exists that contain all the numbers in the range [ 1 , i ] ; Store the position of each element of arr [ ] ; Traverse the array ; Insert the position of arr [ i ] ; Store position of each element from the range [ 1 , N ] ; Iterate over the range [ 1 , N ] ; Insert the index of i into st ; Find the smallest element of st ; Find the largest element of st ; If distance between the largest and smallest element of arr [ ] till i - th index is equal to i ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checksubarrayExist1_N ( int arr [ ] , int N ) { unordered_map < int , int > pos ; for ( int i = 0 ; i < N ; i ++ ) { pos [ arr [ i ] ] = i ; } set < int > st ; for ( int i = 1 ; i <= N ; i ++ ) { st . insert ( pos [ i ] ) ; int Min = * ( st . begin ( ) ) ; int Max = * ( st . rbegin ( ) ) ; if ( Max - Min + 1 == i ) { cout << " True ▁ " ; } else { cout << " False ▁ " ; } } } int main ( ) { int arr [ ] = { 1 , 4 , 3 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; checksubarrayExist1_N ( arr , N ) ; }
Check if a pair of integers A and B can coincide by shifting them by distances arr [ ( A % N + N ) % N ] and arr [ ( B % N + N ) % N ] | C ++ program for the above approach ; Function to check if two integers coincide at a point ; Store the final position of integers A and B ; Iterate over the range [ 0 , n ] ; Store the final position of the integer i ; If temp is present in the Map ; Print Yes and return ; Mark its final position as visited ; If every integer stored in the Map is unique ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkSamePosition ( int arr [ ] , int n ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) { int temp = ( ( i + arr [ i ] ) % n + n ) % n ; if ( mp . find ( temp ) != mp . end ( ) ) { cout << " Yes " ; return ; } mp [ temp ] ++ ; } cout << " No " ; } int main ( ) { int arr [ ] = { 5 , 4 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; checkSamePosition ( arr , N ) ; return 0 ; }
Sum of Fibonacci Numbers | Set 2 | C ++ program for the above approach ; Function to find the sum of first N + 1 fibonacci numbers ; Apply the formula ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sumFib ( int N ) { long num = ( long ) round ( pow ( ( sqrt ( 5 ) + 1 ) / 2.0 , N + 2 ) / sqrt ( 5 ) ) ; cout << ( num - 1 ) ; } int main ( ) { int N = 3 ; sumFib ( N ) ; return 0 ; }
Sum of Fibonacci Numbers | Set 2 | C ++ program for the above approach ; Function to find the sum of first N + 1 fibonacci numbers ; Apply the formula ; Print the result ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sumFib ( int N ) { double num = ( 1 - sqrt ( 5 ) ) / 2 ; long val = round ( abs ( 1 / ( pow ( num , N + 2 ) + pow ( num , N + 1 ) + pow ( num , N ) + pow ( num , N - 1 ) ) ) - 1 ) ; cout << val ; } int main ( ) { int N = 3 ; sumFib ( N ) ; }
Make all array elements equal to K by repeatedly incrementing subsequences | C ++ program for the above approach ; Function to find the minimum number of operations required to make all elements equal to k ; Initialize a hashmap ; Store frequency of array elements ; Store the minimum number of operations required ; Iterate until all array elements becomes equal to K ; Iterate through range [ 1 , k - 1 ] since only one element can be increased from each group ; Check if the current number has frequency > 0 , i . e . , it is a part of a group ; If true , decrease the frequency of current group of element by 1 ; Increase the frequency of the next group of elements by 1 ; If the next element is not the part of any group , then skip it ; Increment count of operations ; Print the count of operations ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minOperations ( int arr [ ] , int n , int k ) { map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) { mp [ arr [ i ] ] ++ ; } int ans = 0 ; while ( mp [ k ] < n ) { for ( int i = 1 ; i <= k - 1 ; i ++ ) { if ( mp [ i ] ) { mp [ i ] -- ; mp [ i + 1 ] ++ ; if ( mp [ i + 1 ] == 1 ) { i ++ ; } } } ans ++ ; } cout << ans ; } int main ( ) { int arr [ ] = { 2 , 3 , 3 , 4 } ; int K = 5 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minOperations ( arr , N , K ) ; return 0 ; }
Minimum number of pigs required to find the poisonous bucket | C ++ program for the above approach ; Function to find the minimum number of pigs required to find the poisonous bucket ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void poorPigs ( int buckets , int minutesToDie , int minutesToTest ) { cout << ceil ( log ( buckets ) / log ( ( minutesToTest / minutesToDie ) + 1 ) ) ; } int main ( ) { int N = 1000 , M = 15 , P = 60 ; poorPigs ( N , M , P ) ; return 0 ; }
Maximum product of the remaining pair after repeatedly replacing pairs of adjacent array elements with their sum | C ++ program for the above approach ; Function to find the maximum product possible after repeatedly replacing pairs of adjacent array elements with their sum ; Store the maximum product ; Store the prefix sum ; Store the total sum of array ; Traverse the array to find the total sum ; Iterate in the range [ 0 , N - 2 ] ; Add arr [ i ] to prefix_sum ; Store the value of prefix_sum ; Store the value of ( total sum - prefix sum ) ; Update the maximum product ; Print the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxProduct ( int arr [ ] , int N ) { int max_product = INT_MIN ; int prefix_sum = 0 ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } for ( int i = 0 ; i < N - 1 ; i ++ ) { prefix_sum += arr [ i ] ; int X = prefix_sum ; int Y = sum - prefix_sum ; max_product = max ( max_product , X * Y ) ; } cout << max_product ; } int main ( ) { int arr [ ] = { 2 , 3 , 5 , 6 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maxProduct ( arr , N ) ; return 0 ; }
Find array elements with rightmost set bit at the position of the rightmost set bit in K | C ++ program for the above approach ; Function to find the mask for finding rightmost set bit in K ; Function to find all array elements with rightmost set bit same as that in K ; Stores mask of K ; Store position of rightmost set bit ; Traverse the array ; Check if rightmost set bit of current array element is same as position of rightmost set bit in K ; Driver Code ; Input ; Function call to find the elements having same rightmost set bit as of K
#include " bits / stdc + + . h " NEW_LINE using namespace std ; int findMask ( int K ) { int mask = 1 ; while ( ( K & mask ) == 0 ) { mask = mask << 1 ; } return mask ; } void sameRightSetBitPos ( int arr [ ] , int N , int K ) { int mask = findMask ( K ) ; int pos = ( K & mask ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] & mask ) == pos ) cout << arr [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 3 , 4 , 6 , 7 , 9 , 12 , 15 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 7 ; sameRightSetBitPos ( arr , N , K ) ; return 0 ; }
Generate an N | C ++ program for the above approach ; Utility function to find x ^ y in O ( log ( y ) ) ; Stores the result ; Update x if it is >= p ; If y is odd ; Multiply x with res ; y must be even now Set y = y / 2 ; Function to generate the N digit number satisfying the given conditions ; Find all possible integers upto 2 ^ N ; Generate binary representation of i ; Reduce the length of the string to N ; If current bit is '0' ; Convert string to equivalent integer ; If condition satisfies ; Print and break ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int power ( long long int x , unsigned long long int y ) { long long int res = 1 ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) ; y = y >> 1 ; x = ( x * x ) ; } return res ; } void printNth ( int N ) { for ( long long int i = 1 ; i <= power ( 2 , N ) ; i ++ ) { string s = bitset < 64 > ( i ) . to_string ( ) ; string s1 = s . substr ( s . length ( ) - N , s . length ( ) ) ; for ( long long int j = 0 ; s1 [ j ] ; j ++ ) { if ( s1 [ j ] == '0' ) { s1 [ j ] = '2' ; } } long long int res = stoll ( s1 , nullptr , 10 ) ; if ( res % power ( 2 , N ) == 0 ) { cout << res << endl ; break ; } } } int main ( ) { int N = 15 ; printNth ( N ) ; }
Maximum sum of K | C ++ program for the above approach ; Function to print the sum of subarray of length K having maximum distinct prime factors ; If K exceeds N ; Stores the count of distinct primes ; True , if index ' i ' is a prime ; Initialize the count of factors to 0 and set all indices as prime ; If i is prime ; Number is prime ; Count of factors of a prime number is 1 ; Increment CountDistinct as the factors of i ; Mark its multiple non - prime ; Compute sum of first K - length subarray ; Compute sums of remaining windows by removing first element of the previous window and adding last element of the current window ; Print the maximum sum ; Driver Code ; Given array ; Given size of subarray ; Size of the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  100001 NEW_LINE int maxSum ( int arr [ ] , int N , int K ) { if ( N < K ) { cout << " Invalid " ; return -1 ; } int CountDistinct [ MAX + 1 ] ; bool prime [ MAX + 1 ] ; for ( int i = 0 ; i <= MAX ; i ++ ) { CountDistinct [ i ] = 0 ; prime [ i ] = true ; } for ( long long int i = 2 ; i <= MAX ; i ++ ) { if ( prime [ i ] == true ) { CountDistinct [ i ] = 1 ; for ( long long int j = i * 2 ; j <= MAX ; j += i ) { CountDistinct [ j ] ++ ; prime [ j ] = false ; } } } int Maxarr_sum = 0 , DistPrimeSum = 0 ; for ( int i = 0 ; i < K ; i ++ ) { Maxarr_sum += arr [ i ] ; DistPrimeSum += CountDistinct [ arr [ i ] ] ; } int curr_sum = DistPrimeSum ; int curr_arrSum = Maxarr_sum ; for ( int i = K ; i < N ; i ++ ) { curr_sum += CountDistinct [ arr [ i ] ] - CountDistinct [ arr [ i - K ] ] ; curr_arrSum += arr [ i ] - arr [ i - K ] ; if ( curr_sum > DistPrimeSum ) { DistPrimeSum = curr_sum ; Maxarr_sum = curr_arrSum ; } else if ( curr_sum == DistPrimeSum ) { Maxarr_sum = max ( curr_arrSum , Maxarr_sum ) ; } } cout << Maxarr_sum ; } int main ( ) { int arr [ ] = { 1 , 4 , 2 , 10 , 3 } ; int K = 3 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maxSum ( arr , N , K ) ; return 0 ; }
Count Pronic numbers from a given range | C ++ program for the above approach ; Function to check if x is a Pronic Number or not ; Check for Pronic Number by multiplying consecutive numbers ; Function to count pronic numbers in the range [ A , B ] ; Initialise count ; Iterate from A to B ; If i is pronic ; Increment count ; Print count ; Driver Code ; Function call to count pronic numbers in the range [ A , B ]
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkPronic ( int x ) { for ( int i = 0 ; i <= ( int ) ( sqrt ( x ) ) ; i ++ ) { if ( x == i * ( i + 1 ) ) { return true ; } } return false ; } void countPronic ( int A , int B ) { int count = 0 ; for ( int i = A ; i <= B ; i ++ ) { if ( checkPronic ( i ) ) { count ++ ; } } cout << count ; } int main ( ) { int A = 3 , B = 20 ; countPronic ( A , B ) ; return 0 ; }
Count Pronic numbers from a given range | C ++ program for the above approach ; Function to count pronic numbers in the range [ A , B ] ; Check upto sqrt N ; If product of consecutive numbers are less than equal to num ; Return N - 1 ; Function to count pronic numbers in the range [ A , B ] ; Subtract the count of pronic numbers which are <= ( A - 1 ) from the count f pronic numbers which are <= B ; Driver Code ; Function call to count pronic numbers in the range [ A , B ]
#include <bits/stdc++.h> NEW_LINE using namespace std ; int pronic ( int num ) { int N = ( int ) sqrt ( num ) ; if ( N * ( N + 1 ) <= num ) { return N ; } return N - 1 ; } int countPronic ( int A , int B ) { return pronic ( B ) - pronic ( A - 1 ) ; } int main ( ) { int A = 3 ; int B = 20 ; cout << countPronic ( A , B ) ; return 0 ; }
Count integers from a given range with no odd divisors | C ++ program for the above approach ; Function to count integers in the range 1 to N having no odd divisors ; Traverse the array ; Stores the nearest power of two less than arr [ i ] ; Stores the count of integers with no odd divisor for each query ; Iterate until powerOfTwo is less then or equal to arr [ i ] ; Print the answer for the current element ; Driver Code ; Given array ; Size of the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void oddDivisors ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int powerOfTwo = 2 ; int count = 0 ; while ( powerOfTwo <= arr [ i ] ) { count ++ ; powerOfTwo = 2 * powerOfTwo ; } cout << count << " ▁ " ; } return ; } int main ( ) { int arr [ ] = { 15 , 16 , 20 , 35 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; oddDivisors ( arr , N ) ; return 0 ; }
Check if all array elements can be reduced to 0 by repeatedly reducing pairs of consecutive elements by their minimum | C ++ program for the above approach ; Function to check if it is possible to convert the array ; Traverse the array range [ 1 , N - 1 ] ; If arr [ i ] < arr [ i - 1 ] ; Otherwise ; Decrement arr [ i ] by arr [ i - 1 ] ; If arr [ n - 1 ] is not equal to zero ; Otherwise ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void checkPossible ( int * arr , int n ) { for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) { cout << " No STRNEWLINE " ; return ; } else { arr [ i ] -= arr [ i - 1 ] ; arr [ i - 1 ] = 0 ; } } if ( arr [ n - 1 ] == 0 ) { cout << " Yes STRNEWLINE " ; } else { cout << " No STRNEWLINE " ; } } int main ( ) { int arr [ ] = { 2 , 3 , 3 , 4 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; checkPossible ( arr , N ) ; return 0 ; }
Nearest smaller power of 2 for every digit of a number | C ++ program to implement the above approach ; Function to find the nearest power of two for every digit of a given number ; Converting number to string ; Traverse the array ; Calculate log base 2 of the current digit s [ i ] ; Highest power of 2 <= s [ i ] ; ASCII conversion ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void highestPowerOfTwo ( int num ) { string s = to_string ( num ) ; for ( int i = 0 ; i < ( int ) s . size ( ) ; i ++ ) { if ( s [ i ] == '0' ) { cout << "0" ; continue ; } int lg = log2 ( int ( s [ i ] ) - 48 ) ; int p = pow ( 2 , lg ) ; cout << char ( p + 48 ) ; } } int main ( ) { int num = 4317 ; highestPowerOfTwo ( num ) ; return 0 ; }
Maximum prefix sum after K reversals of a given array | C ++ program for the above approach ; Function to find the maximum prefix sum after K reversals of the array ; Stores the required sum ; If K is odd , reverse the array ; Store current prefix sum of array ; Traverse the array arr [ ] ; Add arr [ i ] to currsum ; Update maximum prefix sum till now ; Print the answer ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSumAfterKReverse ( int arr [ ] , int K , int N ) { int sum = INT_MIN ; if ( K & 1 ) reverse ( arr , arr + N ) ; int currsum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { currsum += arr [ i ] ; sum = max ( sum , currsum ) ; } cout << sum ; } int main ( ) { int arr [ ] = { 1 , 5 , 8 , 9 , 11 , 2 } ; int K = 1 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maxSumAfterKReverse ( arr , K , N ) ; return 0 ; }
Print all submasks of a given mask | C ++ Program for above approach ; Function to print the submasks of N ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void SubMasks ( int N ) { for ( int S = N ; S ; S = ( S - 1 ) & N ) { cout << S << " ▁ " ; } } int main ( ) { int N = 25 ; SubMasks ( N ) ; return 0 ; }
Count pairs from a given range having equal Bitwise OR and XOR values | C ++ program for the above approach ; Function to calculate ( x ^ y ) % MOD ; Initialize result ; Update x if it is more than or equal to MOD ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Return ( x ^ y ) % MOD ; Function to count total pairs ; The upper bound is 2 ^ N ; Stores the count of pairs ; Generate all possible pairs ; Find XOR of both integers ; Find OR of both integers ; If both are equal ; Increment count ; Print count % MOD ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE #define MOD  1000000007 NEW_LINE using namespace std ; long long int power ( int x , int y ) { long long int res = 1 ; x = x % MOD ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % MOD ; y = y >> 1 ; x = ( x * x ) % MOD ; } return res ; } void countPairs ( int N ) { long long int high = power ( 2 , N ) ; int count = 0 ; for ( int i = 0 ; i < high ; i ++ ) { for ( int j = 0 ; j < high ; j ++ ) { int X = ( i ^ j ) ; int Y = ( i j ) ; if ( X == Y ) { count ++ ; } } } cout << count % MOD << endl ; } int main ( ) { int N = 10 ; countPairs ( N ) ; return 0 ; }
Count pairs from a given range having equal Bitwise OR and XOR values | C ++ program for the above approach ; Function to find the value of ( x ^ y ) % MOD ; Initialize result ; Update x if it is more than or equal to MOD ; If y is odd , multiply x with result ; y must be even now , then update y / 2 ; Return ( x ^ y ) % MOD ; Function to count total pairs ; Finding 3 ^ N % 10 ^ 9 + 7 ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE #define MOD  1000000007 NEW_LINE using namespace std ; long long int power ( int x , int y ) { long long int res = 1 ; x = x % MOD ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % MOD ; y = y >> 1 ; x = ( x * x ) % MOD ; } return res ; } void countPairs ( int N ) { cout << power ( 3 , N ) ; } int main ( ) { int N = 10 ; countPairs ( N ) ; return 0 ; }
Minimize swaps required to place largest and smallest array elements at first and last array indices | C ++ program for the above approach ; Function to find the minimum count of adjacent swaps to move largest and smallest element at the first and the last index of the array , respectively ; Stores the smallest array element ; Stores the smallest array element ; Stores the last occurrence of the smallest array element ; Stores the first occurrence of the largest array element ; Traverse the array arr [ ] ; If a [ i ] is less than min_element ; Update min_element ; Update min_ind ; If a [ i ] is greater than max_element ; Update max_element ; Update max_ind ; If max_ind is equal to min_ind ; Return 0 ; If max_ind is greater than min_ind ; Otherwise ; Driver Code ; Input ; Print the result
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumMoves ( int * a , int n ) { int min_element = INT_MAX ; int max_element = INT_MIN ; int min_ind = -1 ; int max_ind = -1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] <= min_element ) { min_element = a [ i ] ; min_ind = i ; } if ( a [ i ] > max_element ) { max_element = a [ i ] ; max_ind = i ; } } if ( max_ind == min_ind ) { return 0 ; } else if ( max_ind > min_ind ) { return max_ind + ( n - min_ind - 2 ) ; } else { return max_ind + n - min_ind - 1 ; } } int main ( ) { int arr [ ] = { 35 , 46 , 17 , 23 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minimumMoves ( arr , N ) << endl ; }
Find the index of the smallest element to be removed to make sum of array divisible by K | C ++ program to implement the above approach ; Function to find index of the smallest array element required to be removed to make sum divisible by K ; Stores sum of array elements ; Stores index of the smallest element removed from the array to make sum divisible by K ; Stores the smallest element removed from the array to make sum divisible by K ; Traverse the array , arr [ ] ; Update sum ; Traverse the array arr [ ] ; Calculate remaining sum ; If sum is divisible by K ; If res == - 1 or mini is greater than arr [ i ] ; Update res and mini ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findIndex ( int arr [ ] , int n , int K ) { int sum = 0 ; int res = -1 ; int mini = 1e9 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { int temp = sum - arr [ i ] ; if ( temp % K == 0 ) { if ( res == -1 mini > arr [ i ] ) { res = i + 1 ; mini = arr [ i ] ; } } } return res ; } int main ( ) { int arr [ ] = { 14 , 7 , 8 , 2 , 4 } ; int K = 7 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findIndex ( arr , N , K ) ; return 0 ; }
Count subarrays having a single distinct element that can be obtained from a given array | C ++ program for the above approach ; Function to count subarrays of single distinct element into which given array can be split ; Stores the count ; Stores frequency of array elements ; Traverse the array ; Traverse the map ; Increase count of subarrays by ( frequency - 1 ) ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void divisionalArrays ( int arr [ 3 ] , int N ) { int sum = N ; unordered_map < int , int > mp ; for ( int i = 0 ; i < N ; i ++ ) { mp [ arr [ i ] ] ++ ; } for ( auto x : mp ) { if ( x . second > 1 ) { sum += x . second - 1 ; } } cout << sum << endl ; } int main ( ) { int arr [ ] = { 1 , 1 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; divisionalArrays ( arr , N ) ; }
Count inversions in a sequence generated by appending given array K times | C ++ program for the above approach ; Function to count the number of inversions in K copies of given array ; Stores count of inversions in the given array ; Stores the count of pairs of distinct array elements ; Traverse the array ; Generate each pair ; Check for each pair , if the condition is satisfied or not ; If pairs consist of distinct elements ; Count inversiosn in the sequence ; Print the answer ; Driver Code ; Given array ; Given K ; Size of the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void totalInversions ( int arr [ ] , int K , int N ) { int inv = 0 ; int X = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( arr [ i ] > arr [ j ] and i < j ) inv ++ ; if ( arr [ i ] > arr [ j ] ) X ++ ; } } int totalInv = X * K * ( K - 1 ) / 2 + inv * K ; cout << totalInv << endl ; } int main ( ) { int arr [ ] = { 2 , 1 , 3 } ; int K = 3 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; totalInversions ( arr , K , N ) ; }