Distinct adjacent elements in a binary array Last Updated : 07 Dec, 2022 Comments Improve Suggest changes 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 Distinct adjacent elements in a binary array A AbhinavKinagi Follow Improve Article Tags : Algorithms Searching Python Python Programs DSA Arrays binary-representation +3 More Practice Tags : AlgorithmsArrayspythonSearching Similar Reads Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio 10 min read DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth 15+ min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p 11 min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Like