Question

Individually or in pairs, please write pseudo-code to solve the following problems and state which Big-θ...

Individually or in pairs, please write pseudo-code to solve the following problems and state which Big-θ category the algorithm belongs to (you don’t have to prove it).

  1. An array, A[0…n-2], contains n-1 integers from 0 to n-1 in increasing order – one integer is missing in the range. Design the most efficient algorithm you can to find the missing integer.
  2. You have an unsorted sequence of college applications in an array, A[0…n-1]. Each application A contains a name A.name, an SAT score A.sat and a Boolean value indicating whether the student applied early decision, A.early. Design the most efficient algorithm you can to rearrange the array so that all the early decision applicants appear in the first i cells of the array A[0…i-1] and all the remaining applicants appear in the last j cells of the array A[i … n-1].
0 0
Add a comment Improve this question Transcribed image text
Answer #1

I am writing the pseudo - codes individually .

1. we know that the formula for sum of first N positive numbers is (N * (N+1)) / 2 . So , compute the value of sum of first n-1 numbers and place the value in x . Now, compute the sum of all the A[0....n-2] numbers by traversing the array and place the value in y.

The difference between x and y is the missing number .

The time complexity is O(n) , since we need to traverse whole array for finding the sum value .

PSEUDO - CODE :

int x , y ;

x = ( (n-1) * (( n-1) +1) ) / 2 ;

int sum =0;

for(int i=0;i<=n-2;i++)

sum += A[i] ;

y = sum ;

printf("the missing value is : %d ", x-y);

2. Let i and j are two variables which are used for traversing the array A. we initialize i and j to first two indices  in A.

we have conditions to check ;-

Here , i and j corresponds to the index values

a. if i == late and j == early then swap the values in i and j and increment both the values of i and j

b. if i == late and j == late then increment j

c. if i == early and j == late then increment j

d.if i == early and j == early then increment the values of both i and j .

I keep the boolean value 1 for early in A.early and boolean value 0 for late in A.early  

PSEUDO - CODE :

int i = 0 , j=1 ;

for ( ; j < n ; )

{

if (A[i].early == 0 && A[j].early == 1 )

{

swap ((A[i] , A[j]) ;

i++,j++ ;

}

if (A[i].early == 0 && A[j].early == 0 ) j++;

if (A[i].early == 1 && A[j].early == 0 ) j++;

if (A[i].early == 1 && A[j].early == 1 ) i++ , j++ ;

}

Add a comment
Know the answer?
Add Answer to:
Individually or in pairs, please write pseudo-code to solve the following problems and state which Big-θ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Design an algorithm for the following description. Solution can be done in pseudo-code or steps of...

    Design an algorithm for the following description. Solution can be done in pseudo-code or steps of the algorithm. Describe and analyze an algorithm that takes an unsorted array A of n integers (in an unbounded range) and an integer k, and divides A into k equal-sized groups, such that the integers in the first group are lower than the integers in the second group, and the integers in the second group are lower than the integers in the third group,...

  • Your task is to design algorithms to solve the following problems. For full credit, your algorithm...

    Your task is to design algorithms to solve the following problems. For full credit, your algorithm must run in logarithmic time. Given a number n greaterthanorequalto 1 and a (user-specified) error tolerance e, you want to approximate the squareroot of n to within error tolerance e. Specifically, you want to return an x = Squareroot n that satisfies |x^2 - n| greaterthanorequalto e. For example, to compute the squareroot of n = 2 with e = 0.01, an acceptable answer...

  • the question from the course COMP 4040 that Analysis of Algorithms if you want to answer it by code please use C or C++...

    the question from the course COMP 4040 that Analysis of Algorithms if you want to answer it by code please use C or C++ 5. Algorithm Design (20 points) Input: array A contains n distinct numbers from 1 to n, in arbitrary order. Output: number of inversions (defined as the number of pair(i, j) of array indices with i < j and A[i] > Aj]) (a) (5 points) What array with elements from the set {1, 2, ..., n) has...

  • i need the solution in pseudo code please. 4 Dynamic Programmii Consider the following problem based on the transf...

    i need the solution in pseudo code please. 4 Dynamic Programmii Consider the following problem based on the transformation of a sequence (or collection) of coloured disks. Assume that you have a very large collection of disks, each with an integer value representing the disk colour from the range [0, cl. For example, the colour mapping might be: O-red, 1-yellow, 2-blue, 3-pink,. c-black For a given sequence of coloured disks eg.,0,1,2,3,4), at each time step (or iteration) you are only...

  • Please solve using Java and comment your code for clarity. Sample 3. Input: 4 1 231...

    Please solve using Java and comment your code for clarity. Sample 3. Input: 4 1 231 Output: 0 This sequence also does not have a majority element (note that the element 1 appears twice and hence is not a majority element). Problem Introduction Majority rule is a decision rule that selects the alternative which has a majority, that is, more than half the votes. Given a sequence of elements (1.02....,On, you would like to check whether it contains an element...

  • solution is required in pseudo code please. 2 Knapsack Problem În al Knapsack problem. given n items(11-12. . . ....

    solution is required in pseudo code please. 2 Knapsack Problem În al Knapsack problem. given n items(11-12. . . . . 1"} with weight {w1·W2. . . . . ux) and value (n 2, .., nJ, the goal is to select a combination of items such that the total value V is maximized and the total weight is less or equal to a given capacity In this question, we will consider two different ways to represent a solution to the...

  • 1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array...

    1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...

  • I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][]...

    I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][] t) A method that returns true if from left to right in any row, the integers are increasing, otherwise false. - columnValuesIncrease(int[][] t) A method that returns true if from top to bottom in any column, the integers are increasing, otherwise false. - isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n}...

  • Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b,...

    Problem Definition: Problem: Given an array of integers find all pairs of integers, a and b, where a – b is equal to a given number. For example, consider the following array and suppose we want to find all pairs of integers a and b where a – b = 3 A = [10, 4, 6, 16, 1, 6, 12, 13] Then your method should return the following pairs: 4, 1 15, 12 13, 10 A poor solution: There are...

  • The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also...

    The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also please explain why the seg fault is happening. Thank you #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name loadMusicFile // @Brief Load the music database // 'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT