Skip to main content
More descriptive title
Source Link
John L.
  • 39.1k
  • 4
  • 34
  • 91

Array containing Determine whether a sorted array contain at least 4 distinct elements in O(log n) time

On one of my previous courseworks, I was faced with the following problem, which I think is unrealistic when using a direct / straightforward approach that usually algorithms have by leveraging certain data structures like dictionaries, etc.

Design an O(log n) algorithm whose input is a sorted list A. The algorithm should return true if A contains at least 4 distinct elements. Otherwise the algorithm should return false.

My lecturer came up with the following solution:

def ThreeDiff(A):
 if A[0]==A[len(A)-1]:
  return -1

 minind=0
 maxind=len(A)-1

 while maxind-mind>1:
  midind=int((minind+maxind)/2)
  if A[midind]>A[minind] and A[midind]<A[maxind]:
   return midind
  if A[midind]==A[minind]:
   minind=midind
  else
   maxind=midind
return -1

def FourDiff(A):
  midind=ThreeDiff(A)
  if midind==-1:
   return false
  return ThreeDiff(A[0:midind+1])!=- 1 or ThreeDiff(A[midind:len(A)]) != -1
def ThreeDiff(A):
  if A[0]==A[len(A)-1]:
    return -1

  minind=0
  maxind=len(A)-1

  while maxind-mind>1:
    midind=int((minind+maxind)/2)
    if A[midind]>A[minind] and A[midind]<A[maxind]:
      return midind
    if A[midind]==A[minind]:
      minind=midind
    else:
     maxind=midind
  return -1

def FourDiff(A):
  midind=ThreeDiff(A)
  if midind==-1:
    return false
  return ThreeDiff(A[0:midind+1])!=- 1 or ThreeDiff(A[midind:len(A)]) != -1

Is there a cleaner or better way to solve this problem?

Array containing at least 4 distinct elements in O(log n)

On one of my previous courseworks, I was faced with the following problem, which I think is unrealistic when using a direct / straightforward approach that usually algorithms have by leveraging certain data structures like dictionaries, etc.

Design an O(log n) algorithm whose input is a sorted list A. The algorithm should return true if A contains at least 4 distinct elements. Otherwise the algorithm should return false.

My lecturer came up with the following solution:

def ThreeDiff(A):
 if A[0]==A[len(A)-1]:
  return -1

 minind=0
 maxind=len(A)-1

 while maxind-mind>1:
  midind=int((minind+maxind)/2)
  if A[midind]>A[minind] and A[midind]<A[maxind]:
   return midind
  if A[midind]==A[minind]:
   minind=midind
  else
   maxind=midind
return -1

def FourDiff(A):
  midind=ThreeDiff(A)
  if midind==-1:
   return false
  return ThreeDiff(A[0:midind+1])!=- 1 or ThreeDiff(A[midind:len(A)]) != -1

Is there a cleaner or better way to solve this problem?

Determine whether a sorted array contain at least 4 distinct elements in O(log n) time

On one of my previous courseworks, I was faced with the following problem, which I think is unrealistic when using a direct / straightforward approach that usually algorithms have by leveraging certain data structures like dictionaries, etc.

Design an O(log n) algorithm whose input is a sorted list A. The algorithm should return true if A contains at least 4 distinct elements. Otherwise the algorithm should return false.

My lecturer came up with the following solution:

def ThreeDiff(A):
  if A[0]==A[len(A)-1]:
    return -1

  minind=0
  maxind=len(A)-1

  while maxind-mind>1:
    midind=int((minind+maxind)/2)
    if A[midind]>A[minind] and A[midind]<A[maxind]:
      return midind
    if A[midind]==A[minind]:
      minind=midind
    else:
     maxind=midind
  return -1

def FourDiff(A):
  midind=ThreeDiff(A)
  if midind==-1:
    return false
  return ThreeDiff(A[0:midind+1])!=- 1 or ThreeDiff(A[midind:len(A)]) != -1

Is there a cleaner or better way to solve this problem?

added 10 characters in body
Source Link

On one of my previous courseworks, I was faced with the following problem, which I think is unrealistic when using a direct / straightforward approach that usually algorithms have by leveraging certain data structures like dictionaries, etc.

Design an O(log n) algorithm whose input is a sorted list A. The algorithm should return true if A contains at least 4 distinct elements. Otherwise the algorithm should return false.

My lecturer came up with the following solution:

def ThreeDiff(A):
 if A[0]==A[len(A)-1]:
  return -1

 minind=0
 maxind=len(A)-1

 while maxind-mind>1:
  midind=int((minind+maxind)/2)
  if A[midind]>A[minind] and A[midind]<A[maxind]:
   return midind
  if A[midind]==A[minind]:
   minind=midind
  else
   maxind=midind
return -1

def FourDiff(A):
  midind=ThreeDiff(A)
  if midind==-1:
   return false
  return ThreeDiff(A[0:midind+1])!=- 1 or ThreeDiff(A[midind:len(A)]) != -1

Is there a cleaner or better way to solve this problem?

On one of my previous courseworks, I was faced with the following problem, which I think is unrealistic when using a direct / straightforward approach that usually algorithms have by leveraging certain data structures like dictionaries, etc.

Design an O(log n) algorithm whose input is a sorted list A. The algorithm should return true if A contains at least 4 distinct elements. Otherwise the algorithm should return false.

My lecturer came up with the following solution:

def ThreeDiff(A):
 if A[0]==A[len(A)-1]:
  return -1

 minind=0
 maxind=len(A)-1

 while maxind-mind>1:
  midind=int((minind+maxind)/2)
  if A[midind]>A[minind] and A[midind]<A[maxind]:
   return midind
  if A[midind]==A[minind]:
   minind=midind
  else
   maxind=midind
return -1

def FourDiff(A):
  midind=ThreeDiff(A)
  if midind==-1:
   return false
  return ThreeDiff(A[0:midind+1])!=- 1 or ThreeDiff(A[midind:len(A)]) != -1

Is there a cleaner way to solve this problem?

On one of my previous courseworks, I was faced with the following problem, which I think is unrealistic when using a direct / straightforward approach that usually algorithms have by leveraging certain data structures like dictionaries, etc.

Design an O(log n) algorithm whose input is a sorted list A. The algorithm should return true if A contains at least 4 distinct elements. Otherwise the algorithm should return false.

My lecturer came up with the following solution:

def ThreeDiff(A):
 if A[0]==A[len(A)-1]:
  return -1

 minind=0
 maxind=len(A)-1

 while maxind-mind>1:
  midind=int((minind+maxind)/2)
  if A[midind]>A[minind] and A[midind]<A[maxind]:
   return midind
  if A[midind]==A[minind]:
   minind=midind
  else
   maxind=midind
return -1

def FourDiff(A):
  midind=ThreeDiff(A)
  if midind==-1:
   return false
  return ThreeDiff(A[0:midind+1])!=- 1 or ThreeDiff(A[midind:len(A)]) != -1

Is there a cleaner or better way to solve this problem?

added 1 character in body
Source Link

On one of my previous courseworks, I was faced with the following problem, which I think is unrealistic when using a direct / straightforward approach that usually algorithms have by leveraging certain data structures like dictionaries, etc.

Design an O(log n) algorithm whose input is a sorted list A. The algorithm should return true if A contains at least 4 distinct elements. Otherwise the algorithm should return false.

My lecturer came up with the following solution:

def ThreeDiff(A):
 if A[0]==A[len(A)-1]:
  return -1

 minind=0
 maxind=len(A)-1

 while maxind-mind>1:
  midind=int((minind+maxind)/2)
  if A[midind]>A[minind] and A[midind]<A[maxind]:
   return midind
  if A[midind]==A[minind]:
   minind=midind
  else
   maxind=midind
return -1

def FourDiff(A):
  midind=ThreeDiff(A)
  if midind==-1:
   return false
  return ThreeDiff(A[0:midind+1])!=- 1 or ThreeDiff(A[midind:len(A)]) != -1

Is there a cleaner way to solve this problem?

On one of my previous courseworks, I was faced with the following problem, which I think is unrealistic when using a direct / straightforward approach that usually algorithms have by leveraging certain data structures like dictionaries, etc.

Design an O(log n) algorithm whose input is a sorted list A. The algorithm should return true if A contains at least 4 distinct elements. Otherwise the algorithm should return false.

My lecturer came up with the following solution:

def ThreeDiff(A):
 if A[0]==A[len(A)-1]:
  return -1

 minind=0
 maxind=len(A)-1

 while maxind-mind>1:
  midind=int((minind+maxind)/2)
  if A[midind]>A[minind] and A[midind]<A[maxind]:
   return midind
  if A[midind]==A[minind]:
   minind=midind
  else
  maxind=midind
return -1

def FourDiff(A):
  midind=ThreeDiff(A)
  if midind==-1:
   return false
  return ThreeDiff(A[0:midind+1])!=- 1 or ThreeDiff(A[midind:len(A)]) != -1

Is there a cleaner way to solve this problem?

On one of my previous courseworks, I was faced with the following problem, which I think is unrealistic when using a direct / straightforward approach that usually algorithms have by leveraging certain data structures like dictionaries, etc.

Design an O(log n) algorithm whose input is a sorted list A. The algorithm should return true if A contains at least 4 distinct elements. Otherwise the algorithm should return false.

My lecturer came up with the following solution:

def ThreeDiff(A):
 if A[0]==A[len(A)-1]:
  return -1

 minind=0
 maxind=len(A)-1

 while maxind-mind>1:
  midind=int((minind+maxind)/2)
  if A[midind]>A[minind] and A[midind]<A[maxind]:
   return midind
  if A[midind]==A[minind]:
   minind=midind
  else
   maxind=midind
return -1

def FourDiff(A):
  midind=ThreeDiff(A)
  if midind==-1:
   return false
  return ThreeDiff(A[0:midind+1])!=- 1 or ThreeDiff(A[midind:len(A)]) != -1

Is there a cleaner way to solve this problem?

Source Link
Loading