retagged by
4,039 views
2 votes
2 votes
Given a sorted array of n elements where other than one element x every other element repeat two times. Then how much time will it take to find position of x?
retagged by

5 Answers

6 votes
6 votes

Time complexity for this solution is O ( lgn ), you will also find the condition to move left or right here.

see from question I can conclude that array must be of odd length...right

because every element but one is repeated twice, arr[] = 1,1,2,2,3,4,4 --> length = 7

lets start binary search with middle_element_index = floor( 7 / 2 ) = 3

arr[ 3 ] = 2              1,1,2,2,3,4,4

now compare this element with next and previous index element

if arr[3] matched with previous element:       1,1,2,2,3,4,4

           your single element must be on right side of this middle

if arr[3] matched with next element:        

          ( for this condition to satisfy your array must have been like this 1,2,2,3,3,4,4 )

          your single element must be on left side of this middle

else you found that single (bachelor) element who was disturbing other couples wink :

find appropriate side to move, find next middle on that side and repeat procedure

until you either reach end, or both next and previous element different, in that case you found single element

edited by
1 votes
1 votes

digvijay sir is right... first thing...repeated...once...means..112233... repeated twice means...111222333....

if(a[i] != a[i+1)  printf(a[i]); /return 1///first element is single...

      else if (a[j) ! =a[j-1]) printf(a[j]); return 1////last element is single...

Bs(a,i,j,n)  ///                where i is the index of first element..j is the index of last element..                                                                                   n is the no. of elements  in array...'a'   is array adddress

if(n%2==0)/////when 'n' is even 

{    

               mid= (i+j)/2 /////take ceil value

             if(a[mid-1) != a[mid] ! && a[mid] != a[mid+1]) printf(a[mid); return 1////mid element is single..

             else if(a[mid) != a[mid +1] ) 

            {  BS(a, mid+1, j,n)  ////move right

              }

            else if(a[mid-1]== a[mid]a && [mid]== a[mid+1])

           {      BS(a, i ,mid-2)  ////move left

              }

}

else/////when n is odd

{

               mid= (i+j)/2 /////

             if(a[mid-1) != a[mid] != a[mid+1]) printf(a[mid); return 1////mid element is single..

             else if(a[mid) != a[mid -1 ) 

            {     BS(a, mid+3, j,n)  ////move right

            }  else if(a[mid) == a[mid -1 ) 

            {     BS(a, i,mid-3,n)  ////move left

            }  

                

}                complexity is 0(logn)  plz..take input and verify and if any error or facing any problem...ask me

edited by
1 votes
1 votes

let size of array is n and elements in the array are consecutive n-1 integer in which one element is repeated then we can apply Binary Search.

Answer will be O(log n)

0 votes
0 votes

This is possible in O(lg n) time using Divide and Conquer technique.
Below is a C program for the same  
 

#include <stdio.h>

int findNonRepeated(int arr[], int i, int j);

void main() {
    int arr[] = {10, 20, 20, 30, 30, 50, 50, 60, 60};
    int len = sizeof(arr) / sizeof(arr[0]);
    int x = findNonRepeated(arr, 0, len - 1);
    printf("\n X: %d", x);
}

int findNonRepeated(int arr[], int i, int j) {
    // Base Case
    if (i == j) return arr[i];

    // Divide
    int mid = (i + j) / 2;

    // Conquer
    // If left element equals current element
    if (arr[mid - 1] == arr[mid]) {
        // If current index is even search in left side
        if (mid % 2 == 0)
            return findNonRepeated(arr, i, mid - 2);
        else
            return findNonRepeated(arr, mid + 1, j);
    } else if (arr[mid] == arr[mid + 1]) {
        // If current index is even search in right side
        if (mid % 2 == 0)
            return findNonRepeated(arr, mid + 2, j);
        else
            return findNonRepeated(arr, i, mid - 1);
    } else
        return arr[mid];
}

Related questions

1 votes
1 votes
0 answers
1
0 votes
0 votes
1 answer
2
0 votes
0 votes
3 answers
3
0 votes
0 votes
1 answer
4
Deepalitrapti asked Sep 11, 2018
335 views