给定一个由N 个正整数组成的数组arr[] ,任务是计算仅由一位数元素组成的子数组。
例子:
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.
朴素的方法:最简单的方法是遍历数组并生成所有可能的子数组。对于每个子数组,检查其中的所有整数是否都是个位数整数。
时间复杂度: O(N 3 )
辅助空间: O(1)
有效的方法:为了优化上述方法,其思想是找到每个连续个位数整数块的大小,并按该长度的总子数组增加计数。请按照以下步骤解决问题:
- 初始化一个变量,比如res = 0和c = 0 ,以存储子数组的总数和一个子数组中个位数整数的总数。
- 遍历数组并执行以下操作:
- 如果arr[i] < 10,将c的计数加 1,将res的计数加c。
- 否则,分配c = 0。
- 最后,打印个位数整数子数组的总数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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
输出:
6
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live