给定一个大小为N的数组A ,其中数组元素包含从1 到 N的重复值,任务是找到以相同元素开始和结束的子数组的总数。
例子:
Input: A[] = {1, 2, 1, 5, 2}
Output: 7
Explanation:
Total 7 sub-array of the given array are {1}, {2}, {1}, {5}, {2}, {1, 2, 1} and {2, 1, 5, 2} are start and end with same element.
Input: A[] = {1, 5, 6, 1, 9, 5, 8, 10, 8, 9}
Output: 14
Explanation:
Total 14 sub-array {1}, {5}, {6}, {1}, {9}, {5}, {8}, {10}, {8}, {9}, {1, 5, 6, 1}, {5, 6, 1, 9, 5}, {9, 5, 8, 10, 8, 9} and {8, 10, 8} start and end with same element.
朴素的方法:对于数组中的每个元素,如果它也出现在不同的索引处,我们会将结果加 1。此外,所有 1 大小的子数组都包含在结果中。因此,将 N 添加到结果中。
下面是上述方法的实现:
C++
// C++ program to Count total sub-array
// which start and end with same element
#include
using namespace std;
// Function to find total sub-array
// which start and end with same element
void cntArray(int A[], int N)
{
// initialize result with 0
int result = 0;
for (int i = 0; i < N; i++) {
// all size 1 sub-array
// is part of our result
result++;
// element at current index
int current_value = A[i];
for (int j = i + 1; j < N; j++) {
// Check if A[j] = A[i]
// increase result by 1
if (A[j] == current_value) {
result++;
}
}
}
// print the result
cout << result << endl;
}
// Driver code
int main()
{
int A[] = { 1, 5, 6, 1, 9,
5, 8, 10, 8, 9 };
int N = sizeof(A) / sizeof(int);
cntArray(A, N);
return 0;
}
Java
// Java program to Count total sub-array
// which start and end with same element
public class Main {
// function to find total sub-array
// which start and end with same element
public static void cntArray(int A[], int N)
{
// initialize result with 0
int result = 0;
for (int i = 0; i < N; i++) {
// all size 1 sub-array
// is part of our result
result++;
// element at current index
int current_value = A[i];
for (int j = i + 1; j < N; j++) {
// Check if A[j] = A[i]
// increase result by 1
if (A[j] == current_value) {
result++;
}
}
}
// print the result
System.out.println(result);
}
// Driver code
public static void main(String[] args)
{
int[] A = { 1, 5, 6, 1, 9,
5, 8, 10, 8, 9 };
int N = A.length;
cntArray(A, N);
}
}
Python3
# Python3 program to count total sub-array
# which start and end with same element
# Function to find total sub-array
# which start and end with same element
def cntArray(A, N):
# Initialize result with 0
result = 0
for i in range(0, N):
# All size 1 sub-array
# is part of our result
result = result + 1
# Element at current index
current_value = A[i]
for j in range(i + 1, N):
# Check if A[j] = A[i]
# increase result by 1
if (A[j] == current_value):
result = result + 1
# Print the result
print(result)
print("\n")
# Driver code
A = [ 1, 5, 6, 1, 9, 5, 8, 10, 8, 9 ]
N = len(A)
cntArray(A, N)
# This code is contributed by PratikBasu
C#
// C# program to Count total sub-array
// which start and end with same element
using System;
class GFG{
// function to find total sub-array
// which start and end with same element
public static void cntArray(int []A, int N)
{
// initialize result with 0
int result = 0;
for (int i = 0; i < N; i++)
{
// all size 1 sub-array
// is part of our result
result++;
// element at current index
int current_value = A[i];
for (int j = i + 1; j < N; j++)
{
// Check if A[j] = A[i]
// increase result by 1
if (A[j] == current_value)
{
result++;
}
}
}
// print the result
Console.Write(result);
}
// Driver code
public static void Main()
{
int[] A = { 1, 5, 6, 1, 9,
5, 8, 10, 8, 9 };
int N = A.Length;
cntArray(A, N);
}
}
// This code is contributed by Code_Mech
Javascript
C++
// C++ program to Count total sub-array
// which start and end with same element
#include
using namespace std;
// function to find total sub-array
// which start and end with same element
void cntArray(int A[], int N)
{
// initialize result with 0
int result = 0;
// array to count frequency of 1 to N
int frequency[N + 1] = { 0 };
for (int i = 0; i < N; i++) {
// update frequency of A[i]
frequency[A[i]]++;
}
for (int i = 1; i <= N; i++) {
int frequency_of_i = frequency[i];
// update result with sub-array
// contributed by number i
result += +((frequency_of_i)
* (frequency_of_i + 1))
/ 2;
}
// print the result
cout << result << endl;
}
// Driver code
int main()
{
int A[] = { 1, 5, 6, 1, 9, 5,
8, 10, 8, 9 };
int N = sizeof(A) / sizeof(int);
cntArray(A, N);
return 0;
}
Java
// Java program to Count total sub-array
// which start and end with same element
public class Main {
// function to find total sub-array which
// start and end with same element
public static void cntArray(int A[], int N)
{
// initialize result with 0
int result = 0;
// array to count frequency of 1 to N
int[] frequency = new int[N + 1];
for (int i = 0; i < N; i++) {
// update frequency of A[i]
frequency[A[i]]++;
}
for (int i = 1; i <= N; i++) {
int frequency_of_i = frequency[i];
// update result with sub-array
// contributed by number i
result += ((frequency_of_i)
* (frequency_of_i + 1))
/ 2;
}
// print the result
System.out.println(result);
}
// Driver code
public static void main(String[] args)
{
int[] A = { 1, 5, 6, 1, 9, 5,
8, 10, 8, 9 };
int N = A.length;
cntArray(A, N);
}
}
Python3
# Python3 program to count total sub-array
# which start and end with same element
# Function to find total sub-array
# which start and end with same element
def cntArray(A, N):
# Initialize result with 0
result = 0
# Array to count frequency of 1 to N
frequency = [0] * (N + 1)
for i in range(0, N):
# Update frequency of A[i]
frequency[A[i]] = frequency[A[i]] + 1
for i in range(1, N + 1):
frequency_of_i = frequency[i]
# Update result with sub-array
# contributed by number i
result = result + ((frequency_of_i) *
(frequency_of_i + 1)) / 2
# Print the result
print(int(result))
print("\n")
# Driver code
A = [ 1, 5, 6, 1, 9, 5, 8, 10, 8, 9 ]
N = len(A)
cntArray(A, N)
# This code is contributed by PratikBasu
C#
// C# program to Count total sub-array
// which start and end with same element
using System;
class GFG{
// function to find total sub-array which
// start and end with same element
public static void cntArray(int []A, int N)
{
// initialize result with 0
int result = 0;
// array to count frequency of 1 to N
int[] frequency = new int[N + 1];
for (int i = 0; i < N; i++)
{
// update frequency of A[i]
frequency[A[i]]++;
}
for (int i = 1; i <= N; i++)
{
int frequency_of_i = frequency[i];
// update result with sub-array
// contributed by number i
result += ((frequency_of_i) *
(frequency_of_i + 1)) / 2;
}
// print the result
Console.Write(result);
}
// Driver code
public static void Main()
{
int[] A = { 1, 5, 6, 1, 9, 5,
8, 10, 8, 9 };
int N = A.Length;
cntArray(A, N);
}
}
// This code is contributed by Nidhi_Biet
Javascript
14
时间复杂度: O(N 2 ) ,其中 N 是数组的大小
空间复杂度: O(1)
有效的方法:我们可以通过观察答案仅取决于原始数组中数字的频率来优化上述方法。
例如在数组 {1, 5, 6, 1, 9, 5, 8, 10, 8, 9} 中,1 的频率为 2,对答案有贡献的子数组为 {1}、{1} 和 {1, 5, 6, 1} 分别,即一共3个。
因此,计算数组中每个元素的频率。然后对于每个元素,通过以下公式产生的结果增加计数:
((frequency of element)*(frequency of element + 1)) / 2
下面是上述方法的实现:
C++
// C++ program to Count total sub-array
// which start and end with same element
#include
using namespace std;
// function to find total sub-array
// which start and end with same element
void cntArray(int A[], int N)
{
// initialize result with 0
int result = 0;
// array to count frequency of 1 to N
int frequency[N + 1] = { 0 };
for (int i = 0; i < N; i++) {
// update frequency of A[i]
frequency[A[i]]++;
}
for (int i = 1; i <= N; i++) {
int frequency_of_i = frequency[i];
// update result with sub-array
// contributed by number i
result += +((frequency_of_i)
* (frequency_of_i + 1))
/ 2;
}
// print the result
cout << result << endl;
}
// Driver code
int main()
{
int A[] = { 1, 5, 6, 1, 9, 5,
8, 10, 8, 9 };
int N = sizeof(A) / sizeof(int);
cntArray(A, N);
return 0;
}
Java
// Java program to Count total sub-array
// which start and end with same element
public class Main {
// function to find total sub-array which
// start and end with same element
public static void cntArray(int A[], int N)
{
// initialize result with 0
int result = 0;
// array to count frequency of 1 to N
int[] frequency = new int[N + 1];
for (int i = 0; i < N; i++) {
// update frequency of A[i]
frequency[A[i]]++;
}
for (int i = 1; i <= N; i++) {
int frequency_of_i = frequency[i];
// update result with sub-array
// contributed by number i
result += ((frequency_of_i)
* (frequency_of_i + 1))
/ 2;
}
// print the result
System.out.println(result);
}
// Driver code
public static void main(String[] args)
{
int[] A = { 1, 5, 6, 1, 9, 5,
8, 10, 8, 9 };
int N = A.length;
cntArray(A, N);
}
}
蟒蛇3
# Python3 program to count total sub-array
# which start and end with same element
# Function to find total sub-array
# which start and end with same element
def cntArray(A, N):
# Initialize result with 0
result = 0
# Array to count frequency of 1 to N
frequency = [0] * (N + 1)
for i in range(0, N):
# Update frequency of A[i]
frequency[A[i]] = frequency[A[i]] + 1
for i in range(1, N + 1):
frequency_of_i = frequency[i]
# Update result with sub-array
# contributed by number i
result = result + ((frequency_of_i) *
(frequency_of_i + 1)) / 2
# Print the result
print(int(result))
print("\n")
# Driver code
A = [ 1, 5, 6, 1, 9, 5, 8, 10, 8, 9 ]
N = len(A)
cntArray(A, N)
# This code is contributed by PratikBasu
C#
// C# program to Count total sub-array
// which start and end with same element
using System;
class GFG{
// function to find total sub-array which
// start and end with same element
public static void cntArray(int []A, int N)
{
// initialize result with 0
int result = 0;
// array to count frequency of 1 to N
int[] frequency = new int[N + 1];
for (int i = 0; i < N; i++)
{
// update frequency of A[i]
frequency[A[i]]++;
}
for (int i = 1; i <= N; i++)
{
int frequency_of_i = frequency[i];
// update result with sub-array
// contributed by number i
result += ((frequency_of_i) *
(frequency_of_i + 1)) / 2;
}
// print the result
Console.Write(result);
}
// Driver code
public static void Main()
{
int[] A = { 1, 5, 6, 1, 9, 5,
8, 10, 8, 9 };
int N = A.Length;
cntArray(A, N);
}
}
// This code is contributed by Nidhi_Biet
Javascript
14
时间复杂度: O(N) ,其中 N 是数组的大小
空间复杂度: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live