Last updated at October 28, 2026 by Teachoo
Transcript
Ex 11.2, 1 (i) Suppose List 1 and List 2 are two lists of numbers in increasing order. (i) Write an algorithm to find elements in List 1 that are not present in List 2. We must keep an entry only when it belongs to the first list and is absent from the second. Algorithm Start with an empty list only-in-list-1 For each number x in List 1 If x does not appear in List 2, add x to the end of only-in-list-1 Report only-in-list-1 Example Let List 1 = [1, 2, 3, 6, 9, 18] List 2 = [1, 2, 3, 4, 6, 12] Therefore, only-in-list-1 = [9, 18] Entry from List 1 | Is it present in List 2? | only-in-list-1 1 | Yes | [] 2 | Yes | [] 3 | Yes | [] 6 | Yes | [] 9 | No | [9,] 18 | No | [9, 18] Ex 11.2, 1 (ii) Suppose List 1 and List 2 are two lists of numbers in increasing order. (ii) Write an algorithm to find elements in List 2 that are not present in List 1. We must keep an entry only when it belongs to the second list and is absent from the first. Algorithm Start with an empty list only-in-list-2 For each number x in List 2 If x does not appear in List 2, add x to the end of only-in-list-2 Report only-in-list-2 Example Let List 1 = [1, 2, 3, 6, 9, 18] List 2 = [1, 2, 3, 4, 6, 12] Therefore, only-in-list-2 = [4, 12] Entry from List 2 | Is it present in List 1? | only-in-list-2 1 | Yes | [] 2 | Yes | [] 3 | Yes | [] 4 | No | [4,] 6 | Yes | [4,] 12 | No | [4, 12]