Week 4 Assessment. Q : Consider the following code segment. Assume n to be a positive integer.

Similar documents
Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park

Analysis of Binary Search algorithm and Selection Sort algorithm

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Sources: On the Web: Slides will be available on:

Analysis of a Search Algorithm

SOLVING POLYNOMIAL EQUATIONS

We can express this in decimal notation (in contrast to the underline notation we have been using) as follows: b + 90c = c + 10b

Playing with Numbers

Pseudo code Tutorial and Exercises Teacher s Version

Section IV.1: Recursive Algorithms and Recursion Trees

Unit Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

PUTNAM TRAINING POLYNOMIALS. Exercises 1. Find a polynomial with integral coefficients whose zeros include

10CS35: Data Structures Using C

Senem Kumova Metin & Ilker Korkmaz 1

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Number Systems and Radix Conversion

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Roots of Polynomials

6. Standard Algorithms

Sample Questions Csci 1112 A. Bellaachia

a 11 x 1 + a 12 x a 1n x n = b 1 a 21 x 1 + a 22 x a 2n x n = b 2.

Real Roots of Univariate Polynomials with Real Coefficients

Positional Numbering System

Cardinality. The set of all finite strings over the alphabet of lowercase letters is countable. The set of real numbers R is an uncountable set.

6 Commutators and the derived series. [x,y] = xyx 1 y 1.

Class : MAC 286. Data Structure. Research Paper on Sorting Algorithms

PES Institute of Technology-BSC QUESTION BANK

Regular Languages and Finite State Machines

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Chapter 3. if 2 a i then location: = i. Page 40

St S a t ck a ck nd Qu Q eue 1

2) Based on the information in the table which choice BEST shows the answer to 1 906?

it is easy to see that α = a

recursion here it is in C++ power function cis15 advanced programming techniques, using c++ fall 2007 lecture # VI.1

The finite field with 2 elements The simplest finite field is

C++ Programming Language

Computational Geometry. Lecture 1: Introduction and Convex Hulls

Factoring Polynomials

Algorithms. Margaret M. Fleck. 18 October 2010

Notes on Complexity Theory Last updated: August, Lecture 1

Symbol Tables. Introduction

IMPROVING PERFORMANCE OF RANDOMIZED SIGNATURE SORT USING HASHING AND BITWISE OPERATORS

Data Structures. Topic #12

B.Sc.(Computer Science) and. B.Sc.(IT) Effective From July 2011

AP Computer Science Java Mr. Clausen Program 9A, 9B

What Is Recursion? Recursion. Binary search example postponed to end of lecture

6.1 Add & Subtract Polynomial Expression & Functions

SAT Math Facts & Formulas Review Quiz

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Application. Outline. 3-1 Polynomial Functions 3-2 Finding Rational Zeros of. Polynomial. 3-3 Approximating Real Zeros of.

Sequence of Numbers. Mun Chou, Fong QED Education Scientific Malaysia

Hash Tables. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Chapter 4: Computer Codes

1 if 1 x 0 1 if 0 x 1

Lecture 18: Applications of Dynamic Programming Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

Tim Kerins. Leaving Certificate Honours Maths - Algebra. Tim Kerins. the date

Lecture 3: Finding integer solutions to systems of linear equations

Algebra II End of Course Exam Answer Key Segment I. Scientific Calculator Only

APP INVENTOR. Test Review

Informatica e Sistemi in Tempo Reale

Computer Science 2nd Year Solved Exercise. Programs 3/4/2013. Aumir Shabbir Pakistan School & College Muscat. Important. Chapter # 3.

1 Lecture: Integration of rational functions by decomposition

Matrix Algebra. Some Basic Matrix Laws. Before reading the text or the following notes glance at the following list of basic matrix algebra laws.

The Set Data Model CHAPTER What This Chapter Is About

This unit will lay the groundwork for later units where the students will extend this knowledge to quadratic and exponential functions.

4/1/2017. PS. Sequences and Series FROM 9.2 AND 9.3 IN THE BOOK AS WELL AS FROM OTHER SOURCES. TODAY IS NATIONAL MANATEE APPRECIATION DAY

Module 816. File Management in C. M. Campbell 1993 Deakin University

ML for the Working Programmer

Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; }

Binary Heap Algorithms

SYSTEMS OF EQUATIONS AND MATRICES WITH THE TI-89. by Joseph Collison

CP Lab 2: Writing programs for simple arithmetic problems

Passing 1D arrays to functions.

Lecture 6: Finite Fields (PART 3) PART 3: Polynomial Arithmetic. Theoretical Underpinnings of Modern Cryptography

Differentiation and Integration

Why? A central concept in Computer Science. Algorithms are ubiquitous.

DNA Data and Program Representation. Alexandre David

Python Lists and Loops

The Division Algorithm for Polynomials Handout Monday March 5, 2012

11 Multivariate Polynomials

a 1 x + a 0 =0. (3) ax 2 + bx + c =0. (4)

Copy in your notebook: Add an example of each term with the symbols used in algebra 2 if there are any.

8 Polynomials Worksheet

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

Advanced Oracle SQL Tuning

March 29, S4.4 Theorems about Zeros of Polynomial Functions

Basics of Polynomial Theory

Data Structures and Algorithms Written Examination

Notes 11: List Decoding Folded Reed-Solomon Codes

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Introduction to Data Structures

OPTIMAL BINARY SEARCH TREES

Ordered Lists and Binary Trees

Computer Science 210: Data Structures. Searching

Transcription:

Week 4 Assessment Question 1 : Q : Consider the following code segment. Assume n to be a positive integer. for(i=1; i<=n; i++) { for(j=1; j<=n; j=j+i) { printf( Hi ); Let T(n) represent the total running time of the above code segment. Specify which of the following statements are correct. T(n)=O(n^2) T(n)=O(n*log n) T(n) O(n^4) T(n)=O(log n) A : T(n) = O(nlogn) and T(n) = O(n^2). In the i-th iteration, the inner loop runs O(n/i) times, and therefore, the overall complexity is O(n+n/1+n/2+...+1), and this is O(nlogn). Question 2 : Q : Let P(x)=3x^2 + 4x + 2 be a polynomial in the variable x. If Horner s rule is used, what is the number of multiplications to compute P(10)? A : 2 multiplications. By Horner s rule P n (x) = a n and P i (x) = a i + x * P i+1 (x). To find P(k), we need to calculate P 0 (k). P 0 (10) = 2 + 10 * (4 + 10 * 3) = 342. Question 3 : Q : To multiply two six digit numbers what is the number of single digit multiplication operations performed by Karatsuba s Algorithm? A : 12 multiplications. By applying karatsuba s multiplication, we find the product of two 3 digit numbers recursively, ie 4 + 4 = 8 multiplications. Then we combine the results using 4 more multiplications, hence 8 + 4 = 12 multiplications in total.

Question 4 : Q : What is the degree of the polynomial defined by R(x) = P(x) * Q(x) where P(x) = x^3+x^2+1 and Q(x) = x^2+ x + 1? A : The degree of a polynomial is the highest degree of its terms. In R(x), the term with the highest degree will we obtained by multiplying x^3 of P(x) and x ^ 2 of Q(x). So, the degree is 5. Question 5 : Q : For the following array and the specified search key, what will be the number of comparisons performed by unordered linear search? array = { 10, 7, 3, 9, 11, 18, 15, -6, 25, 9, 16, 19 search key = 15 A : The element 15 is 7th position. So, an unordered linear search will take 7 comparisons. Question 6 : Q : For the following array and the specified search key, what will be the number of iterations performed by binary search? A : array = {-6, 3, 4, 7, 9, 10, 11, 15, 16, 18, 19, 22, 25, 33, 47 search key = 12 1st comparison : {-6, 3, 4, 7, 9, 10, 11, 15, 16, 18, 19, 22, 25, 33, 47 2nd comparison : {-6, 3, 4, 7, 9, 10, 11 3rd comparison : {9, 10, 11 4th comparison : {11 Binary search would exit failing to find 12 as there are no more elements to search. The answer therefore is 4. Question 7 : Q : Can linear search take lesser number of comparisons than binary search to find an element in an array?

A : Yes. A linear search takes only one comparison to search for the first element whereas with binary search it is at least 1 (1 when the size of an array is 1, > 1 otherwise). Question 8 : Q : Given the two arrays below which are to be merged into a single array sorted in ascending order, total number of comparisons required to merge these arrays will be: A1 = (10, 20, 30, 40, 50, 60) A2 = (15, 25, 35, 45) A : 8 comparisons are needed to merge the first 8 elements. After merging the first 8 elements into an array M : M = {10, 15, 20, 25, 30, 35, 40, 45 A1 = {50, 60 A2 = { At this point, the elements in A1 can be added into M without any comparisons. Therefore the total number of comparisons required are 8. Question 9 : Q : You are given a sorted array A of size 200 elements. Five values are appended at the end of the array. Which sorting algorithm would be a better candidate for sorting the whole array? A : Let the size of the array initially be n. k elements are added at the end. When using insertion sort, only the last 5 elements have to be inserted into appropriate locations. Doing so, would result in atmost n + (n + 1) + (n + 2).. + (n + k -1) swaps, which is in O(kn). But, merge sort still takes O((n + k) log (n + k)) steps to sort. When k is significantly smaller than n ( k << n) as is the case here (5 and 200), insertions sort takes O(n) steps, whereas merge sort takes O(n log n) step. Hence, insertion sort is preferred. Question 10 : Q : Suppose that the range of values k in counting sort is not of O(n). Is counting sort still guaranteed to sort in linear time?

A : No. In counting sort, a collection of objects are sorted according to keys that are small integers. Counting sort uses these integers (keys, in general) as indices into an array. The size of the array could only defined when the range of the values is in O(n). Question 11 : Q : Does counting sort use comparisons for sorting? A : There are no comparisons involved. It uses integers as indices into an array. Question 12 : Q : If radix sort uses an unstable sorting algorithm to sort the digits in radix sort, is it still guaranteed to work correctly? A : No. For example, consider sorting 13, 12. Sorting the numbers based on the least significant digit : 12, 13. Sorting the numbers based on the next significant digit : 12, 13 In this step, the sorting algorithm will just see 1 and 1. If the sorting algorithm is not stable, it could output 13, 12 which is wrong. So, unless it is stable, the order that is created by the previous steps is lost. Question 13 : Q : Let the numbers 794, 332, 561, 342, 200, 607, and 893 be sorted using radix sort. What will be the sixth number in the sequence of numbers after sorting the second digit? A : The numbers to be sorted are 794, 332, 561, 342, 200, 607, and 893. Sorting based on the least significant digit : 200, 561, 332, 342, 893, 794, 607 Sorting the numbers based on the next significant digit : 200, 607, 332, 342, 561, 893, 794 The answer therefore is 893. Question 14 :

Q : Find median (arithmetic mean if more than one median) for the given array. A = { 10, 30, 20, 17, 25, 28, 13, 27 A : Number of elements in the array are even. Upon ordering the array {10, 13, 17, 20, 25, 27, 28, 30, the median is the mean of 20, 25 which is 22.5. Question 15 : Q : Find median (arithmetic mean if more than one median) for the given array. A = { 10, 30, 20, 25, 28, 13, 27 A : Number of elements in the array are odd. The middlemost element in the ordered array {10, 13, 20, 25, 27, 28, 30 is the median which is 25.

Week 4 Programming Assignments Question 1: You are given two words (not necessarily meaningful words) over the lower case English alphabet. They are to be merged into a single new word in which all the letters (including repetitions) in the given two words occur in increasing order ( a is the least and z is the largest) from left to right. Example: If the two words are ehllo and wrold, the output is dehllloorw. Explanation: d, e, h, l, o, r, w is the increasing order among the distinct letters. Since all the letters must occur in the output word, the output is dehllloorw. Input: Two words, one on each line. Output: The merged word as described in the problem statement. Constraints: 0 < length of each word <= 10. Hint: Study the sorting algorithms mentioned in the lectures and pick the one which best suits this problem. Test Cases: Public Test Cases: Input hello world test case is it Output dehllloorw aceesstt iist Private Test Cases: Input abc abcd abc abc Output aabbccd aabbcc

ace bdf xyzxyzxyzx abcabcabca abcdef aaaabbbcccxxxxyyyzzz Solution: include <stdio.h> #define MAXLEN 10 void printchar(char c, int times) { int i; for (i = 0; i < times; i++) printf("%c", c); int main() { /* Buffers to hold 2 words */ char word1[maxlen + 1], word2[maxlen + 1]; /* The array used for counting sort */ /* Chars can be treated as integers */ int hash[26] = {0; int i = 0; // Iterator /* Read both the lines */ scanf("%s", word1); scanf("%s", word2); for (i = 0; i < MAXLEN + 1 && word1[i]!= '\0'; i++) hash[word1[i] - 'a']++; for (i = 0; i < MAXLEN + 1 && word2[i]!= '\0'; i++) hash[word2[i] - 'a']++; for (i = 0; i < 26; i++) printchar(i + 'a', hash[i]); printf("\n");

return 0;

Question 2: Note: A video solution to this problem may be found here: https://www.youtube.com/watch?v=de9f0caeygy You are given a set of words over the lowercase English alphabet. They are to be sorted in alphabetical order. Alphabetical order: It is the order in which words are organized in a dictionary or in an telephone directory. For example, abc comes before xab. Reason : The first letters differ. So, they are compared. a comes before x in English alphabet. aab comes before abc. Reason : The first letters are same (aab abc). So, the next 2 letters are compared (aab abc). aab comes before aaba. Reason : The first 3 letters in aab are same as the first 3 letters in aaba and there are no more letters in aab to compare against aaba. So, the word with the smaller length comes first (aab). Input: The first line has n, the number of words, followed by n lines each of which contains a word. Output: Words in alphabetical order, one one each line. Constraints: 0 < n <=100, 0 < length of each word <= 10 Hint: See how two-dimensional arrays can help. Test Cases: Public Test Cases: Input 2 hello world 2 antelope ant Output hello world ant antelope

5 which was when while were was were when which while Private Test Cases: Input 1 a 2 aabbccddee aabbccddef 4 d ce bfg ahij 3 abc ab a 6 bca cab bac acb abc cba Output a aabbccddee aabbccddef ahij bfg ce d a ab abc abc aca bac bca cab cba Solution: #include <stdio.h> #define MAXWORDS 100 #define WORDLENGTH 10 /* Function to swap two integers */ void swap(int *a, int *b) {

int t = *a; *a = *b; *b = t; /* Function to compare two strings */ int compare(char *s1, char *s2) { while (*s1 && (*s1 == *s2)) { s1++; s2++; return *s1 - *s2; int main() { int n, i = 0, j = 0; char words[maxwords][wordlength + 1]; /* We ll be manipulating order rather than words */ /* It is easier this way, we need not move words */ int order[maxwords] = {0; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%s", words[i]); order[i] = i; /* Standard insertion sort */ for (i = 1 ; i < n; i++) { j = i; while (j > 0 && compare(words[order[j-1]], words[order[j]]) > 0) { swap (&order[j], &order[j-1]); j--; for (i = 0; i < n - 1; i++)

printf("%s\n", words[order[i]]); printf("%s", words[order[i]]); return 0;

Question 3 : A polynomial of degree n is of the form P(x) = a n x n + a n-1 x n-1 + + a 0. Given two polynomials f(x) and g(x) of degrees n and m respectively, write a program to find the polynomial h(x) given by, h(x) = f(x) * g(x) Input: Line 1 contains n and m separated by space. Line 2 contains the coefficients a n, a n-1, a 0 of f(x) separated by space. Line 3 contains the coefficients b m, b m-1, b 0 of g(x) separated by space. Output: The degree of h(x) followed by the coefficients c k, c k-1, c 0 of h(x) in next line separated by space. Sample Input: 2 2 1 2 3 3 2 1 Sample Output: 4 3 8 14 8 3 Constraints: 1 <= n <= 10 1 <= a i <= 100 Test Cases: Public Test Cases: Input 2 2 1 2 3 3 2 1 2 3 1 1 1 1 1 1 1 Output 4 3 8 14 8 3 5 1 2 3 3 2 1

3 3 1 0 1 0 1 1 0 1 6 1 1 1 2 0 1 0 Private Test Cases: Input 4 4 10 20 30 40 50 50 40 30 20 10 10 10 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 5 6 2 3 4 5 6 7 9 8 7 6 5 4 3 6 5 71 72 73 74 75 76 77 90 91 92 93 94 95 1 1 5 0 5 0 Output 8 500 1400 2600 4000 5500 4000 2600 1400 500 20 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11 18 43 74 110 150 193 166 135 104 74 46 21 11 6390 12941 19654 26530 33570 40775 41330 34865 28232 21430 14458 7315 2 25 0 0 Solution: // Program - Polynomial Multiplication. #include <stdio.h> // Polynomial Multiplication. void polynomialmultiply(int n, int m, int *a, int *b) { int c[30] = {0; // Initialize. for(int i = 0; i <= n; ++i) for(int j = 0; j <= m; ++j) c[i + j] += a[i] * b[j];

// Print. printf("%d\n", n + m); for(int i = 0; i <= n + m; ++i) { printf("%d", c[i]); if(i < n + m) printf(" "); // int main() { int n, m; int a[15], b[15]; scanf("%d %d", &n, &m); for(int i = 0; i <= n; ++i) scanf("%d", &a[i]); for(int i = 0; i <= m; ++i) scanf("%d", &b[i]); polynomialmultiply(n, m, a, b); return 0;

Question 4: You are given a polynomial of degree n. The polynomial is of the form P(x) = a n x n + a n- 1x n-1 + + a 0. For given values k and m, You are required to find P(k) at the end of the m th iteration of Horner s rule. The steps involved in the Horner s rule are given below, P n (x) = a n P n-1 (x) = a n-1 + x * P n (x) P n-2 (x) = a n-2 + x * P n-1 (x).. P 0 (x) = a 0 + x * P 1 (x) 1 st iteration. 2 nd iteration. n th iteration. In general, P i (x) = a i + x * P i + 1 (x) and P 0 (x) is the final result. The input to your program is as follows, Line 1 contains the integers n, m and k separated by space. Line 2 contains the coefficients a n, a n-1, a 0 separated by space. Input: Integers n, m, k and the coefficients as described above. Output: P(k) value at the end of the m th iteration. Sample Input: 2 2 5 3 2 1 Sample Output: 86 Constraints: 1 <= n, k, m <= 10 0 <= a i <=10 Test Cases: Public test cases: Input 2 2 5 3 2 1 Output 86

3 2 2 4 3 2 1 3 1 5 3 1 0 1 24 16 Private test cases: Input 2 1 1 1 0 5 4 4 2 5 4 3 2 1 5 3 10 5 6 7 8 9 10 4 3 10 6 7 8 9 10 4 1 1 1 0 0 1 1 Output 1 129 5678 6789 1 Solution: // Program - Horner's Rule. #include <stdio.h> int main() { int n, m, k, a[30]; int ans; scanf("%d %d %d", &n, &m, &k); ans = 0; for(int i = 0; i <= n; ++i) scanf("%d", &a[i]); // Find the m'th iteration value.

ans = a[0]; for(int i = 1; i <= m; ++i) ans = a[i] + k * ans; printf("%d", ans); return 0;