给定大小为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
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);
}
}
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
Java脚本
14
时间复杂度: O(N) ,其中N是数组的大小
空间复杂度: O(N)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。