Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Count subarrays made up of single-digit integers only

  • Last Updated : 31 Mar, 2021

Given an array arr[] consisting of N positive integers, the task is to count subarrays consisting of single-digit elements only.

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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 only
int 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 Code
int 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 approach
class GFG
{
 
// Function to count of subarrays made
// up of single digit integers only
static 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 Code
public 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 only
def 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 Code
if __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 approach
using System;
class GFG{
 
// Function to count of subarrays made
// up of single digit integers only
static 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 Code
public 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 only
function 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 array
let arr = [ 0, 1, 14, 2, 5 ];
 
// Size of the array
let N = arr.length;
 
singleDigitSubarrayCount(arr, N);
 
// This code is contributed by Manoj.
 
</script>
Output: 
6

 

Time Complexity: O(N)
Auxiliary Space: O(1) 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!