0% found this document useful (0 votes)
113 views

C++ Programming Practice With Solution

The document provides solutions to coding problems involving time complexity analysis and algorithms to solve problems involving arrays. For problem 1, it analyzes the time complexities of 5 code snippets, ranging from O(1) to O(N^2). For problem 2, it presents an O(N log N) algorithm to find a unique element in a list where all others occur in pairs. For problem 3, it provides 3 algorithms in pseudocode to count pairs in an array with a given difference K, all with O(N log N) time complexity.

Uploaded by

Junaid Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views

C++ Programming Practice With Solution

The document provides solutions to coding problems involving time complexity analysis and algorithms to solve problems involving arrays. For problem 1, it analyzes the time complexities of 5 code snippets, ranging from O(1) to O(N^2). For problem 2, it presents an O(N log N) algorithm to find a unique element in a list where all others occur in pairs. For problem 3, it provides 3 algorithms in pseudocode to count pairs in an array with a given difference K, all with O(N log N) time complexity.

Uploaded by

Junaid Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment # 2 - Solution

Problem 1:
Find the Time Complexities of the following C++ codes. Assume that there is no syntax error.

(a) Code 1:

int Sum = 0;
for (int i=1; i<N; i *= 2)
{
for (int j=N; j>0; j /= 3)
Sum++;
}

Θ(log2N)

(b) Code 2:

int Sum = 0;
for (int i=0; i<N; i++)
{
for (int j=0; j<N; j++)
If (j<0)
Sum++;
}

Θ(N2)

(c) Code 3:

int Sum = 0;
for (int i=0; i<N; i=i+2)
{
for (int j=0; j<i; j++)
Sum++;
}

Θ(N2)
(d) Code 4:

int Sum = 0;
for (int i=0; i<N; i++)
{
if ( i < (N/2) )
for (int j=0; j<N; j*=2)
Sum++;
}

This is not an algorithm (due to the inner infinite loop)

(e) Code 5:

int Sum = 0;
for (int i=1; i<N; i++)
{
if ( (i%5) == 0 )
Sum = 10;
break;
}

Θ(1)

For Problems 2 & 3, design efficient algorithms (in pseudocode form). Find time complexity function and asymptotic
time complexity of the proposed algorithms. Also, write best case, average case, and worst case for the proposed
algorithms.

Problem 2:
There are N integers in a list A. All integers except one integer occur in pairs (the integers in the pair may not be
adjacent i.e. the two numbers in a pair can separately exist anywhere in the list). Write an algorithm (in pseudocode
form) to find out the number that occurs only once.

Here is the logic of some solutions.


1. Sort the array in O(NlogN) time using Merge or Quick sort. Then find the element which
does not exist in pair in O(N).
2. A trivial comparison-based algorithm of O(N2) also exist.

Problem 3:
Given N integers, write an algorithm (in pseudocode form) to count the total pairs of integers that have a difference
of K.
Inputs
Number of integers N, the difference K, and the list A of elements.
Output
One integer telling the number of pairs that have a difference of K.
Solution 1:

A trivial comparison based algorithm of O(N2) exists.

Solution 2:

Initialize count as 0
Sort all numbers in increasing order.
Remove duplicates from array.
Do following for each element arr[i]
a) Binary Search for arr[i] + k in subarray from i+1 to n-1.
b) If arr[i] + k found, increment count.
Return count.

The sorting algorithm takes O(nLogn) time.


It runs binary search n times, so the time complexity is O(nLogn).
Therefore, the overall time complexity is O(nLogn).

Solution 3:

Initialize count as 0.
Insert all elements of arr[] in an AVL tree. While inserting, ignore an element if already
present in AVL tree.
Do following for each element arr[i].
Search for arr[i] + k in AVL tree, if found then increment count.
Search for arr[i] - k in AVL tree, if found then increment count.
Remove arr[i] from AVL tree.

Time complexity of above solution is also O(nLogn) as search and delete operations
take O(Logn) time for a self-balancing binary search tree.

You might also like