logo

Answers to some programming questions

4 Pages1164 Words159 Views
   

Added on  2019-09-22

About This Document

This article provides answers to some programming questions related to complexity, recursion, binomial coefficient etc. It includes detailed explanations and examples for better understanding.

Answers to some programming questions

   Added on 2019-09-22

ShareRelated Documents
~~~~~~~~~~~~~~~~~~Answer 1: ~~~~~~~~~~~~~~~~~Complexity = O(n^2)Function to compute sum is (n/2)^2;Proof : 1 + 3 + 5 + 7 + 9 + 11 + 2(n-1) = n^2so : 1 + 3 + 5 + 7 + 9 + n-1 = (n/2)^2;Some Example : 10 - 2515 - 49100 - 25001000 – 250000Prog :publicclass Test2 {publicstaticvoid main(String[] args) {System.out.println(func(10000));}publicstaticint func(intn) {intsum = 0;n = n - ( n % 2 );for (intk = 1; k < n; k += 2)for (intj = 0; j < k; ++j)++sum;returnsum;}}~~~~~~~~~~~~~~~Answer 2: ~~~~~~~~~~~~~~~~~~~~No of comparison is same in the all the cases.It will be 2(n-1)Complexity: O(n).As Every element is getting compared 2 times except the first element A[0].So Complexity is O (2(n-1)) which is equivalent to O (n)~~~~~~~~~~~~~~~ Answer 3: ~~~~~~~~~~~~~~~~~~~~
Answers to some programming questions_1
It will be printed 55 Times.Make the recursion tree to understand more f(4*4*4*4*4) —1. f(4*4*4*4*2) — so on2. f(4*4*4*4*1) — so onProgram to verify publicclass Test1 {privatestaticintcount = 0;publicstaticvoid main(String[] args) {System.out.println("Starting....");fun(4*4*4*4*4);System.out.println(count);}privatestaticvoid fun(intn) {if(n<2) {return;}if(n==2) {count++;System.out.println("Hii");}fun(n/2);fun(n/4);}}~~~~~~~~~~~~ Answer 4 : ~~~~~~~~~~~~~~~~~~~~~~Binomail Cofficient:C(n, k) = C(n-1 , k-1) + C(n-1, k) C(n,0) = C(n,n) = 1 // Base conditionRecursive Function:int binomialCoeff(int n, int k){// Base Cases
Answers to some programming questions_2

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents
Java Assignment Help | Online Homework
|13
|1219
|353

Introduction to Programming with Java - Solved Assignments
|12
|1129
|91

Top-Down Approach for Palindrome Partitioning Problem
|3
|984
|331

IntListMethodsTest Class in Java
|2
|715
|415

Java Assignment Codes
|52
|5341
|240

PriorityThreadQueue and ListManager
|4
|670
|24