Count subarrays made up of single-digit integers only
Given an array arr[] consisting of N positive integers, the task is to count subarrays consisting of single-digit elements only.
Examples:
Input: arr[] = {0, 1, 14, 2, 5}
Output: 6
Explanation: All subarrays made of only single digit numbers are {{0}, {1}, {2}, {5}, {0, 1}, {2, 5}}. Therefore, the total count of subarrays is 6.Input: arr[] ={12, 5, 14, 17}
Output: 1
Explanation: All subarrays made of only single digit numbers are {5}.
Therefore, the total count of subarrays is 1.
Naive Approach: The simplest approach is to traverse the array and generate all possible subarrays. For each subarray, check if all integers in it are single-digit integers or not.
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to find the size of each block of contiguous single-digit integers and increment the count by total subarrays of that length. Follow the steps below to solve the problem:
- Initialize a variable, say res = 0 and c = 0, to store the total count of subarrays and the total count of single-digit integers in a subarray.
- Traverse the array and perform the following operations:
- If arr[i] < 10, increment the count of c by one and count of res by c.
- Otherwise, assign c = 0.
- Finally, print the total count of single-digit integer subarrays.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Function to count of subarrays made// up of single digit integers onlyint singleDigitSubarrayCount(int arr[], int N){ // Stores count of subarrays int res = 0; // Stores the count of consecutive // single digit numbers in the array int count = 0; // Traverse the array for (int i = 0; i < N; i++) { if (arr[i] <= 9) { // Increment size of block by 1 count++; // Increment res by count res += count; } else { // Assign count = 0 count = 0; } } cout << res;}// Driver Codeint main(){ // Given array int arr[] = { 0, 1, 14, 2, 5 }; // Size of the array int N = sizeof(arr) / sizeof(arr[0]); singleDigitSubarrayCount(arr, N); return 0;} |
Java
// Java program for the above approachclass GFG{// Function to count of subarrays made// up of single digit integers onlystatic void singleDigitSubarrayCount(int arr[], int N){ // Stores count of subarrays int res = 0; // Stores the count of consecutive // single digit numbers in the array int count = 0; // Traverse the array for (int i = 0; i < N; i++) { if (arr[i] <= 9) { // Increment size of block by 1 count++; // Increment res by count res += count; } else { // Assign count = 0 count = 0; } } System.out.print(res);}// Driver Codepublic static void main(String[] args){ // Given array int arr[] = { 0, 1, 14, 2, 5 }; // Size of the array int N = arr.length; singleDigitSubarrayCount(arr, N);}}// This code is contributed by 29AjayKumar |
Python3
# Python3 program for the above approach# Function to count of subarrays made# up of single digit integers onlydef singleDigitSubarrayCount(arr, N): # Stores count of subarrays res = 0 # Stores the count of consecutive # single digit numbers in the array count = 0 # Traverse the array for i in range(N): if (arr[i] <= 9): # Increment size of block by 1 count += 1 # Increment res by count res += count else: # Assign count = 0 count = 0 print (res)# Driver Codeif __name__ == '__main__': # Given array arr = [0, 1, 14, 2, 5] # Size of the array N = len(arr) singleDigitSubarrayCount(arr, N) # This code is contributed by mohit kumar 29. |
C#
// C# program for the above approachusing System;class GFG{// Function to count of subarrays made// up of single digit integers onlystatic void singleDigitSubarrayCount(int[] arr, int N){ // Stores count of subarrays int res = 0; // Stores the count of consecutive // single digit numbers in the array int count = 0; // Traverse the array for (int i = 0; i < N; i++) { if (arr[i] <= 9) { // Increment size of block by 1 count++; // Increment res by count res += count; } else { // Assign count = 0 count = 0; } } Console.Write(res);}// Driver Codepublic static void Main(string[] args){ // Given array int[] arr = { 0, 1, 14, 2, 5 }; // Size of the array int N = arr.Length; singleDigitSubarrayCount(arr, N);}}// This code is contributed by sanjoy_62. |
Javascript
<script>// Javascript program for the above approach// Function to count of subarrays made// up of single digit integers onlyfunction singleDigitSubarrayCount(arr, N){ // Stores count of subarrays let res = 0; // Stores the count of consecutive // single digit numbers in the array let count = 0; // Traverse the array for(let i = 0; i < N; i++) { if (arr[i] <= 9) { // Increment size of block by 1 count++; // Increment res by count res += count; } else { // Assign count = 0 count = 0; } } document.write(res);}// Driver Code// Given arraylet arr = [ 0, 1, 14, 2, 5 ];// Size of the arraylet N = arr.length;singleDigitSubarrayCount(arr, N);// This code is contributed by Manoj.</script> |
6
Time Complexity: O(N)
Auxiliary Space: O(1)


Please Login to comment...