Question

In: Computer Science

Suppose you are given an integer c and an array, A, indexed from 1 to n,...

Suppose you are given an integer c and an array, A, indexed from 1 to n, of n integers in the range from 0 to 5n (possibly with duplicates). i.e. 0 <= A[i ] <= 5n " I = {1, .., n}.

a.) Write an efficient algorithm that runs in O(n) time in a pseudo code for determining if there are two integers, A[i] and A[j], in A whose sum is c, i.e. c = A[i] + A[j], for 1 <= i < j <= n. Your algorithm should return a set of any pair of those indices (i , j). If there were no such integers, return (0, 0).

b.) Implement your algorithm of Q7 either in Python programming language where A[1:10] = [30, 25, 10, 50, 35, 45, 40, 5, 15, 20] and c = 40.

Solutions

Expert Solution

a)

Function containsIntegers(array: A[], int: ar_size, int: c)
1) Sort the array in non-decreasing order.
2) Initialize two index variables to find the candidate elements in the sorted array.
       (a) Initialize first to the leftmost index: l = 0
       (b) Initialize second  the rightmost index:  r = ar_size - 1
3) Loop while l < r.
       (a) If (A[l] + A[r] == sum)  then return (L, R)
       (b) Else if( A[l] + A[r] <  sum )  then l++
       (c) Else r--    
4) No candidates in whole array -> return (0, 0)

CODE

def containsIntegers(A, arr_size, c):

  quickSort(A, 0, arr_size-1)

  l = 0

  r = arr_size-1

  

  # traverse the array for the two elements

  while l<r:

    if (A[l] + A[r] == c):

      return (l, r)

    elif (A[l] + A[r] < c):

      l += 1

    else:

      r -= 1

  return (0, 0)

def quickSort(A, si, ei):

  if si < ei:

    pi = partition(A, si, ei)

    quickSort(A, si, pi-1)

    quickSort(A, pi + 1, ei)

def partition(A, si, ei):

  x = A[ei]

  i = (si-1)

  for j in range(si, ei):

    if A[j] <= x:

      i += 1      

      A[i], A[j] = A[j], A[i]

    A[i + 1], A[ei] = A[ei], A[i + 1]

  return i + 1

  

A = [30, 25, 10, 50, 35, 45, 40, 5, 15, 20]

c = 40

l, r = containsIntegers(A, len(A), c)

if (l != 0 or r != 0):

  print("%d, %d" %(l, r))

else:

  print("Array doesn't have two elements with the given sum")


Related Solutions

in C++ Description: For a given integer n > 1, the smallest integer d > 1...
in C++ Description: For a given integer n > 1, the smallest integer d > 1 that divides n is a prime factor. We can find the prime factorization of n if we find d and then replace n by the quotient of n divided by d, repeating this until n becomes 1.             Write a program that determines the prime factorization of n in this manner, but that displays the prime factors in descending order. (When you find a...
Given a positive integer k and an array A[1..n] that contains the quiz scores of n...
Given a positive integer k and an array A[1..n] that contains the quiz scores of n students in ascending order, design a divide and conquer algorithm to efficiently count the number of students that have quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. Let group i be the set of students with quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. The counting result should be stored in G[1..k],...
Q#1 Write a C++ program that reads n integer values and stores them in an array...
Q#1 Write a C++ program that reads n integer values and stores them in an array of maximum 20 integers. Read from the user a valid value of n (n should not exceed 20). After reading the n array elements find the maximum and minimum elements. Q#2 Write a C++ program that reads 10 integer values and stores them in an array. The program should find and display the average of the array elements and how many elements are below...
Given an unsorted integer array A of size n, develop an pseudocode with time complexity as...
Given an unsorted integer array A of size n, develop an pseudocode with time complexity as low as possible to find two indexes i and j such that A[i] + A[j] = 100. Indexes i and j may or may not be the same value.
(8%) Write a C/C++ program that takes an input (array) from 1 to n (say n...
(8%) Write a C/C++ program that takes an input (array) from 1 to n (say n = 50) and displays the string representations of those numbers with following conditions If the current number is divisible by 2, then print CSU If the current number is divisible by 5, then print SB If the current number is divisible by both 2 and 5, then print CSUSB If the number is neither divisible by 2 nor 5, then print the number Example:...
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array...
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time. Return that integer. Input: arr = [1,2,2,6,6,6,6,7,10] Output:
Array with Pointers Find Continuous Sub-Array C++ Problem: Given an unsorted array A of size N...
Array with Pointers Find Continuous Sub-Array C++ Problem: Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to the given number. Declare dynamic arrays and use only pointers syntax (no [ ]’s or (ptr+i) stuff.     Input will be the number of input values to enter followed by the sum to compare with. Print out the continuous sub-array of values that are equal to sum or the message ‘No sum found’. There...
Python - You are given a sorted (from smallest to largest) array A of n distinct...
Python - You are given a sorted (from smallest to largest) array A of n distinct integers which can be positive, negative or zero. Design the fastest algorithm you can for deciding if there is an index i such that A[i] = i.
*C PROGRAMMING LANGUAGE* a) Given an array of size n, sort the array using pointers using...
*C PROGRAMMING LANGUAGE* a) Given an array of size n, sort the array using pointers using malloc or calloc. Examples: Input: n = 5, A= {33,21,2,55,4} Output: {2,4,21,33,55} b) Write a function that declares an array of 256 doubles and initializes each element in the array to have a value equal to half of the index of that element. That is, the value at index 0 should be 0.0, the value at index 1 should be 0.5, the value at...
5. Suppose you are given an n-element array containing distinct integers that are listed in increasing...
5. Suppose you are given an n-element array containing distinct integers that are listed in increasing order. Given a number k, describe a recursive algorithm to find two integers in A that sum to k, if such a pair exists. What is the running time of your algorithm? Java Language....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT