Distinct adjacent elements in a binary array Last Updated : 07 Dec, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a binary array arr[] of 1's and 0's of length N. The task is to find the number of elements that are different with respect to their neighbors. Note: At least one of the neighbors should be distinct. Examples: 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 Approach: Run a loop for all the elements of the 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: C++ // C++ implementation of // the above approach #include <bits/stdc++.h> 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 JavaScript <script> // JavaScript implementation of // the above approach function distinct(arr, n) { let count = 0; // if array has only one element, return 1 if (n == 1) return 1; for ( let 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 let arr = [0, 0, 0, 0, 0, 1, 0]; let n = arr.length; document.write(distinct(arr, n)); </script> Output3 Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Check if all elements of binary array can be made 1 A AbhinavKinagi Follow Improve Article Tags : DSA binary-representation Similar Reads Check if an array contains only one distinct element Given an array arr[] of size N, the task is to check if the array contains only one distinct element or not. If it contains only one distinct element then print âYesâ, otherwise print âNoâ. Examples: Input: arr[] = {3, 3, 4, 3, 3} Output: No Explanation: There are 2 distinct elements present in the 8 min read Modify a binary array to Bitwise AND of all elements as 1 Given an array, a[] consists of only 0 and 1. The task is to check if it is possible to transform the array such that the AND value between every pair of indices is 1. The only operation allowed is to: Take two indices i and j and replace the a[i] and a[j] with a[i] | a[j] where '|' means bitwise OR 4 min read Print sorted distinct elements of array Given an array that might contain duplicates, print all distinct elements in sorted order. Examples: Input : 1, 3, 2, 2, 1Output : 1 2 3Input : 1, 1, 1, 2, 2, 3Output : 1 2 3The simple Solution is to sort the array first, then traverse the array and print only first occurrences of elements. Algorith 6 min read Print sorted distinct elements of array Given an array that might contain duplicates, print all distinct elements in sorted order. Examples: Input : 1, 3, 2, 2, 1Output : 1 2 3Input : 1, 1, 1, 2, 2, 3Output : 1 2 3The simple Solution is to sort the array first, then traverse the array and print only first occurrences of elements. Algorith 6 min read Check if all elements of binary array can be made 1 Given a binary array Arr and an integer K. If the value at index i is 1 you can change 0 to 1 with in the range of ( i - K ) to ( i + K ).The task is to determine whether all the elements of the array can be made 1 or not.Examples: Input: Arr = { 0, 1, 0, 1 }, K = 2 Output: 2 Input: Arr = { 1, 0, 0, 7 min read Count pairs formed by distinct element sub-arrays Given an array, count number of pairs that can be formed from all possible contiguous sub-arrays containing distinct numbers. The array contains positive numbers between 0 to n-1 where n is the size of the array. Examples: Input: [1, 4, 2, 4, 3, 2] Output: 8 The subarrays with distinct elements are 7 min read Like