In this article, we will explain everything about finding the number of subarrays whose sum is k^m, m >= 0 in C. Given an array arr[] and an integer K, we need to find the number of subarrays with sums of the form K^m where m is greater than or equal to 0, or we can say we need to find the number of subarrays with sums of the form K^m The sum of the quantities is equal to some non-negative power of K.
Input: arr[] = { 2, 2, 2, 2 } K = 2 Output: 8 Sub-arrays with below indexes are valid: [1, 1], [2, 2], [3, 3], [4, 4], [1, 2], [2, 3], [3, 4], [1, 4] Input: arr[] = { 3, -6, -3, 12 } K = -3 Output: 3
There are mainly two methods-
In this method we will loop through all sub-arrays and check if they are K or not; if so , then we increment the count.
#include <bits/stdc++.h> #define MAX 1000000 using namespace std; int main(){ int arr[] = {2, 2, 2, 2}; // given array int k = 2; // given integer int n = sizeof(arr) / sizeof(arr[0]); // the size of our array int answer = 0; // counter variable for(int i = 0; i < n; i++){ int sum = 0; for(int j = i; j < n; j++){ // this will loop will make all the subarrays sum += arr[j]; int b = 1; while(b < MAX && sum > b) // k^m Max should be 10^6 b *= k; if(b == sum) // if b == sum then increment count answer++; } } cout << answer << "\n"; }
8
However, this method is not very good because the time complexity of the program is O(N*N*log (K)), , where N is the size of the array and K is the size of the array. An integer given by the user.
This complexity is not good because this complexity can be used for higher constraints because if the constraints are large it takes too much time to process so we will try another approach, So that we can use this program to achieve higher constraints.
In this method, we will use prefix sum and mapping to reduce processing, thereby greatly reducing time complexity. < /p>
#include <bits/stdc++.h> #define ll long long #define MAX 1000000 using namespace std; int main(){ int arr[] = {2, 2, 2, 2}; // The given array int n = sizeof(arr) / sizeof(arr[0]); // size of our array int k = 2; // given integer ll prefix_sum[MAX]; prefix_sum[0] = 0; partial_sum(arr, arr + n, prefix_sum + 1); // making prefix sum array ll sum; if (k == 1){ // we are going to check separately for 1 sum = 0; map<ll, int> m; for (int i = n; i >= 0; i--){ // If m[a+b] = c, then add c to the current sum. if (m.find(prefix_sum[i] + 1) != m.end()) sum += m[prefix_sum[i] + 1]; // Increase count of prefix sum. m[prefix_sum[i]]++; } cout << sum << "\n"; } else if (k == -1){ // we are going to check separately for -1 sum = 0; map<ll, int> m; for (int i = n; i >= 0; i--){ // If m[a+b] = c, then add c to the current sum. if (m.find(prefix_sum[i] + 1) != m.end()) sum += m[prefix_sum[i] + 1]; if (m.find(prefix_sum[i] - 1) != m.end()) sum += m[prefix_sum[i] - 1]; // Increase count of prefix sum. m[prefix_sum[i]]++; } cout << sum << "\n"; } else{ sum = 0; ll b; map<ll, int> m; for (int i = n; i >= 0; i--){ b = 1; while (b < MAX){ // we are not going to check for more than 10^6 // If m[a+b] = c, then add c to the current sum. if (m.find(prefix_sum[i] + b) != m.end()) sum += m[prefix_sum[i] + b]; b *= k; } m[prefix_sum[i]]++; } cout << sum << "\n"; } return 0; }
8
We solved a problem to find the number of subarrays whose sum is of the form k^m, where m >= 0, the time complexity is O(nlog(k)log(n)) time complexity. We also learned a C program to solve this problem and a complete way to solve this problem (normal and efficient). We can write the same program in other languages such as C, java, python and other languages. Hope this article is helpful to you.
The above is the detailed content of Written in C++, find the number of subarrays whose sum is k^m, where m >= 0. For more information, please follow other related articles on the PHP Chinese website!