给定长度为N的1和0的二进制数组arr [] 。任务是找到相对于其邻居而言不同的元素数量。
注意:至少一个邻居应该是不同的。
例子:
Input : N = 4 , arr=[1, 0, 1, 1]
Output : 3
arr[0]=1 is distinct since it’s neighbor arr[1]=0 is different.
arr[1]=0 is also distinct, as it has two different neighbors i.e, arr[2]=1 & arr[0]=1.
arr[2]=1 has same neighbor in arr[3]=1 but has different neighbor in arr[1]=0. So it’s distinct.
But arr[3]=1 is not distinct as it’s neighbor arr[2]=1 is the same.
So total distinct elements are 1+1+1+0=3
Input : N = 2 , arr=[1, 1]
Output : 0
方法:
- 对list的所有元素运行一个循环,并将每个元素与其上一个和下一个邻居进行比较。如果元素不同,则递增1。
- 第一个元素仅需与其下一个邻居进行比较,类似地,最后一个元素仅需与其前一个元素进行比较。
- 其余元素有两个邻居。如果两个邻居中的任何一个都不相同,则认为它是不同的。
下面是上述方法的实现:
C++
// C++ implementation of
// the above approach
#include
using namespace std;
int distinct(int arr[], int n)
{
int count = 0;
// if array has only one element, return 1
if (n == 1)
return 1;
for ( int i = 0; i < n - 1; i++)
{
// For first element compare
// with only next element
if(i == 0)
{
if(arr[i] != arr[i + 1])
count += 1;
}
// For remaining elements compare with
// both prev and next elements
else
{
if(arr[i] != arr[i + 1] ||
arr[i] != arr[i - 1])
count += 1;
}
}
// For last element compare
// with only prev element
if(arr[n - 1] != arr[n - 2])
count += 1;
return count;
}
// Driver code
int main()
{
int arr[] = {0, 0, 0, 0, 0, 1, 0};
int n = sizeof(arr) / sizeof(arr[0]);
cout << distinct(arr, n);
return 0;
}
Java
// Java implementation of
// the above approach
class GFG
{
static int distinct(int []arr, int n)
{
int count = 0;
// if array has only one element,
// return 1
if (n == 1)
return 1;
for (int i = 0; i < n - 1; i++)
{
// For first element compare
// with only next element
if(i == 0)
{
if(arr[i] != arr[i + 1])
count += 1;
}
// For remaining elements compare with
// both prev and next elements
else
{
if(arr[i] != arr[i + 1] ||
arr[i] != arr[i - 1])
count += 1;
}
}
// For last element compare
// with only prev element
if(arr[n - 1] != arr[n - 2])
count += 1;
return count;
}
// Driver code
public static void main(String[] args)
{
int arr[] = {0, 0, 0, 0, 0, 1, 0};
int n = arr.length;
System.out.println(distinct(arr, n));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 implementation of
# the above approach
def distinct(arr):
count = 0
# if array has only one element, return 1
if len(arr) == 1:
return 1
for i in range(0, len(arr) - 1):
# For first element compare
# with only next element
if(i == 0):
if(arr[i] != arr[i + 1]):
count += 1
# For remaining elements compare with
# both prev and next elements
elif(i > 0 & i < len(arr) - 1):
if(arr[i] != arr[i + 1] or
arr[i] != arr[i - 1]):
count += 1
# For last element compare
# with only prev element
if(arr[len(arr) - 1] != arr[len(arr) - 2]):
count += 1
return count
# Driver code
arr = [0, 0, 0, 0, 0, 1, 0]
print(distinct(arr))
# This code is contributed by Mohit Kumar
C#
// C# implementation of
// the above approach
using System;
class GFG
{
static int distinct(int []arr, int n)
{
int count = 0;
// if array has only one element,
// return 1
if (n == 1)
return 1;
for (int i = 0; i < n - 1; i++)
{
// For first element compare
// with only next element
if(i == 0)
{
if(arr[i] != arr[i + 1])
count += 1;
}
// For remaining elements compare with
// both prev and next elements
else
{
if(arr[i] != arr[i + 1] ||
arr[i] != arr[i - 1])
count += 1;
}
}
// For last element compare
// with only prev element
if(arr[n - 1] != arr[n - 2])
count += 1;
return count;
}
// Driver code
public static void Main(String[] args)
{
int []arr = {0, 0, 0, 0, 0, 1, 0};
int n = arr.Length;
Console.WriteLine(distinct(arr, n));
}
}
// This code is contributed by Princi Singh
输出:
3
时间复杂度: O(N)