Given a binary array arr of 1’s and 0’s of length N. The task is to find the number of elements which are different with respect to their neighbors.
Note:At least one of the neighbors should be distinct.
Input : N = 4 , arr=[1, 0, 1, 1]
Output : 3
arr=1 is distinct since it’s neighbor arr=0 is different.
arr=0 is also distinct, as it has two different neighbors i.e, arr=1 & arr=1.
arr=1 has same neighbor in arr=1 but has different neighbor in arr=0. So it’s distinct.
But arr=1 is not distinct as it’s neighbor arr=1 is the same.
So total distinct elements are 1+1+1+0=3
Input : N = 2 , arr=[1, 1]
Output : 0
- Run a loop for all the elements of list and compare every element with its previous and next neighbors. Increment count by 1 if the element is distinct.
- The first element has to be compared only with its next neighbor and similarly the last element has to be compared only with its previous element.
- The remaining elements have two neighbors . If anyone of two neighbors is different then it is considered distinct.
Below is the implementation of the above approach:
Time Complexity: O(N)
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.
- Distinct adjacent elements in an array
- Minimize the cost to make all the adjacent elements distinct in an Array
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Count of array elements which is smaller than both its adjacent elements
- Length of array pair formed where one contains all distinct elements and other all same elements
- Split an array containing N elements into K sets of distinct elements
- Remove all 1s from the adjacent left of 0s in a Binary Array
- Maximum set bit sum in array without considering adjacent elements
- Count distinct elements after adding each element of First Array with Second Array
- Minimum adjacent swaps required to Sort Binary array
- Permutation of Array such that sum of adjacent elements are not divisible by 3
- Maximum sum in circular array such that no two elements are adjacent
- Maximum sum in circular array such that no two elements are adjacent | Set 2
- Convert one array to another using adjacent swaps of elements
- Permutation of Array such that products of all adjacent elements are even
- Maximise sum of absolute difference between adjacent elements in Array with sum K
- Minimize the maximum difference between adjacent elements in an array
- Find the original Array using XOR values of all adjacent elements
- Search an element in an array where difference between adjacent elements is 1
- Count distinct elements in an array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.