Separate 0’s and 1’s in an array.

    by nikoo28

    Question: Given an array arr[], that contains only 0’s and 1’s. Write a function that separate 0’s and 1’s in the array.

    Input: 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1
    Output: 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1

    The objective of the problem is to segregate the array in two parts, and it is more popularly known as a variation of Dutch National Flag Problem.

    Method 1:

    • The most naive method is to count the number of elements in the given array. Let it be N.
    • Do a second scan of the array and count the number of zeroes. Let it be c.
    • Now fill the array with c number of 0’s and fill the remaining positions with 1.

    Time Complexity:- O(2n)

    Method 2:

    We can solve this problem in a fashion similar to separating odd and even numbers in an array.

    • Initialize 2 indexes, left = 0 and right = size-1.
    • Increment left, till it is 0.
    • Decrement right till it is 1.
    • If (left < right) swap the values at arr[left] and arr[right].
    void swap(int *i, int *j)
    {
    	int temp = *i;
    	*i = *j;
    	*j = temp;
    }
    
    int seperateZeroAndOne(int arr[], int size)
    {
    	// Initialize left and right indexes
    	int left = 0;
    	int right = size - 1;
    	
    	while(left < right)
    	{
    		// Increment left index till the number is 0
    		while(arr[left] == 0 && left < right)
    			left++;
    		
    		// Decrement right index till the number is 1
    		while(arr[right] == 1 && left < right)
    			right--;
    		
    		// Time to swap
    		if(left < right)
    		{
    			swap(&arr[left], &arr[right]);
    			left++;
    			right--;
    		}
    	}
    }
    

    Time Complexity:- O(n)

    0 comments
    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    Separate odd and even numbers in an array.

    by nikoo28
    2 minutes read

    Question: Given an array arr[], write a function that separates even and odd numbers. The function should print first all the even numbers and then the odd numbers. Input: 4, 8, 15, 16, 23, 42 Output: 4, 8, 16, 42, 23, 15 The objective of this problem is mainly to segregate the array in two halves. One half contains all the even numbers and one half contains all the negative numbers. Naive Method:- The most naive method is to allocate a separate empty array and then we fill it as …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Theory

    The keyword ‘struct’ is optional in C++

    by nikoo28
    1 minutes read

    In C, struct keyword must be used for declaring structure variables, but it is optional in C++. For example, following program gives error in C and works in C++. struct node { int x; node *next; // Error in C, struct must be there. Works in C++ }; int main() { node a; // Error in C, struct must be there. Works in C++ } And following program works in both C and C++. struct node { int x; struct node *next; // Works in both C and C++ }; …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: An element is a majority if it appears more than n/2 times. Give an algorithm that takes an array of n elements and finds the majority element in that array. The array is not sorted. Input: 8, 16, 8, 4, 8, 8, 8, 42, 16, 15, 8, 23, 4, 8, 15, 8, 8. Output: Majority Element = 8 We discussed the basic 2 methods to approach this problem here:- Find majority element. (Method 1) Find majority element. (Method 2 , using sorting) It is recommended to have a look …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: An element is a majority if it appears more than n/2 times. Give an algorithm that takes an array of n elements and finds the majority element in that array. The array is not sorted. Input: 8, 16, 8, 4, 8, 8, 8, 42, 16, 15, 8, 23, 4, 8, 15, 8, 8. Output: Majority Element = 8 We discussed the most naive method to achieve this in our previous post. To improve the time complexity of our problem we can apply sorting technique. If we sort the array, …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    Find the majority element in an array.

    by nikoo28
    1 minutes read

    Question: An element is a majority if it appears more than n/2 times. Give an algorithm that takes an array of n elements and finds the majority element in that array. The array is not sorted. Input: 8, 16, 8, 4, 8, 8, 8, 42, 16, 15, 8, 23, 4, 8, 15, 8, 8. Output: Majority Element = 8 The most basic method of solving this problem is to take two loops and maintain a count of each of the element. If the count reaches more than (n/2) for any …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given a sorted array of n elements, possibly with duplicates, find the number of occurrences of an element. Input: 4, 4, 8, 8, 8, 15, 16, 23, 23, 42. Find 8 Output: Count = 3 We discussed the basic method to find the number of occurrences in this post. But here, in both the cases, the time complexity is not good. We can use some of the tricks studied earlier to find a time efficient method. What we can do is:- Find the first occurrence of the element. Find …

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More