Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. (a) i goes from 1 to n and for each i, j goes from 1 to n. Hence total number of steps = n*n. Hence, complexity is O(n2)
  2.  
  3. (b) The number of steps is 1 + 2 + 3 + ... + n = n*(n+1)/2 = O(n2)
  4.  
  5. (c) i = 1, 2, 4, 8, .. , [log2(n)], where [.] = GIF and j = 1, 2, .., n. Hence number of steps, in the worst case are log2(n)*n, i.e. the complexity is O(n*log2(n))
  6.  
  7. (d) i = 1, 2, ..., [log2(n)] and j = 1,2, .., i. Number of steps is 1 + 2 + 4 + 8 + .. + log2(n). Hence complexity is O(log2(n)2)
  8. -----------------------------------------------------------------------------------------------------------------------------
  9. import java.util.List;
  10. import java.util.ArrayList;
  11.  
  12. public class listRem {
  13.  
  14. static void deletePositions(List<Character> L1, List<Integer> L2) {
  15. int eleRe = 0;
  16. for (Integer L : L2) {
  17. if (L > L1.size()) {
  18. continue;
  19. } else {
  20. L1.remove(L - 1 - eleRe);
  21. eleRe++;
  22. }
  23. }
  24.  
  25. }
  26.  
  27. public static void main(String[] args) {
  28. List<Character> al = new ArrayList<>();
  29. List<Integer> al1 = new ArrayList<>();
  30. al.add('A');
  31. al.add('B');
  32. al.add('C');
  33. al.add('D');
  34. al.add('E');
  35. al1.add(2);
  36. al1.add(4);
  37. al1.add(8);
  38.  
  39. deletePositions(al, al1);
  40.  
  41. System.out.println("Modified ArrayList : " + al);
  42. }
  43. }
  44. ----------------------------------------------------------------------------------------------
  45. // We will need 2 Queues to implement a Stack...
  46. import java.util.*;
  47.  
  48. class GfG {
  49.  
  50. static class StackQ<T> {
  51. // Two inbuilt queues
  52. Queue<T> queue1 = new LinkedList<>();
  53. Queue<T> queue2 = new LinkedList<>();
  54.  
  55. // To maintain current number of
  56. // elements
  57. static int current_size;
  58.  
  59. StackQ()
  60. {
  61. // initialise the current size to 0
  62. current_size = 0;
  63. }
  64.  
  65. void push(T x)
  66. {
  67. current_size++;
  68.  
  69. // Push x first in empty q2
  70. queue2.add(x);
  71.  
  72. // Push all the remaining
  73. // elements in q1 to q2.
  74. while (!this.queue1.isEmpty()) {
  75. queue2.add(queue1.peek());
  76. queue1.remove();
  77. }
  78.  
  79. // swap the names of two queues
  80. Queue<T> q = queue1;
  81. queue1 = queue2;
  82. queue2 = q;
  83. }
  84.  
  85. void pop()
  86. {
  87. // if no elements are there in q1
  88. if (this.queue1.isEmpty())
  89. return;
  90. queue1.remove();
  91. current_size--;
  92. }
  93.  
  94. T topEl()
  95. {
  96. if (this.queue1.isEmpty())
  97. return null;
  98. return this.queue1.peek();
  99. }
  100.  
  101. int size()
  102. {
  103. return current_size;
  104. }
  105. }
  106.  
  107. // driver code to test the class
  108. public static void main(String[] args)
  109. {
  110. StackQ<Integer> myStack = new StackQ<>();
  111. myStack.push(1);
  112. myStack.push(2);
  113. myStack.push(3);
  114.  
  115. System.out.println("current size: " + myStack.size());
  116. System.out.println("The Stack Elements are:-");
  117. System.out.println(myStack.topEl());
  118. myStack.pop();
  119. System.out.println(myStack.topEl());
  120. myStack.pop();
  121. System.out.println(myStack.topEl());
  122.  
  123. System.out.println("current size: " + myStack.size());
  124. }
  125. }
  126. --------------------------------------------------------------------------------
  127. a)Recursive program for finding Binomial Coefficient in JAVA:
  128.  
  129. import java.util.*;
  130. public class SolutionA {
  131. public static void main(String args[])
  132. {
  133. Scanner sc=new Scanner(System.in);
  134. int n=sc.nextInt();
  135. int k=sc.nextInt();
  136. sc.close();
  137. System.out.println(binomialCoefficientRecursive(n,k));
  138.  
  139. }
  140. public static long binomialCoefficientRecursive(int n, int k)
  141. { if(k==0 || k==n)
  142. return 1;
  143. return binomialCoefficientRecursive(n-1,k-1)+binomialCoefficientRecursive(n-1,k);
  144.  
  145.  
  146. }
  147. }
  148.  
  149. 1) The computation is too long for C(40,20) and C(50,25), but it is working fine for C(30,15) with normal computational time.
  150.  
  151. b)Iterative program for finding Binomial Coefficient in JAVA:
  152.  
  153. import java.util.Scanner;
  154. public class SolutionB {
  155. static long a=1,l=1,x;
  156. public static void main(String args[])
  157. {
  158. Scanner sc=new Scanner(System.in);
  159. int n=sc.nextInt();
  160. int k=sc.nextInt();
  161. sc.close();
  162. System.out.println(binomialCoefficientIterative(n,k));
  163.  
  164. }
  165. public static long binomialCoefficientIterative(int n, int k)
  166. {
  167.  
  168. for(int i=k;i>=1;i--)
  169. {
  170. a=a*n;
  171. n=n-1;
  172. l=l*i;
  173.  
  174. }
  175. x=a/l;
  176. return x;
  177.  
  178. }
  179. }
  180.  
  181. The recursive code is effectively working fine for all the inputs whereas the iterative code is working fine for smaller inputs only.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement