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

DSA Assignment 1 Solution

Uploaded by

anshuraghav1204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

DSA Assignment 1 Solution

Uploaded by

anshuraghav1204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Solution of Assignment 1

Question 1 was https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/

Solution 1 Link –

https://leetcode.com/problems/kids-with-the-greatest-number-of-
candies/submissions/1374528119/

Time Complexity: O(n)

Space Complexity: O(n)

Screenshot:

Description:

Time Complexity: O(n)

Iterating to the entire n-sized array and for each array element compared to greatestNo in first
loop that will be O(n)

And in second loop again Iterating to the entire n-sized array and for each array element sum
with extraCandies compared to greatestNo will be O(n)

Total Time Complexity: O(n) + O(n) = O(n)

Space complexity: O(n)

The resultArray stores the results for each kid, so it will have the same length as the candies
array, which is n. so Space Complexity: O(n)

The variables greatestNo and len use constant space, O(1).


Question 2 was https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-
k/description/

Solution 2 Link –

https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-
k/submissions/1374541996/

Time Complexity: O(n2)

Space Complexity: O(1)

Screenshot:

Description:
Time Complexity: O(n2)
Iterating to the entire n-sized array and again Iterating to the (n-i) sized array then
perform the operation that will be O(n*n) = O(n2)
Space complexity: O(1)
No Extra space required for the Program Execution
Question 3 was https://leetcode.com/problems/find-common-elements-between-two-arrays/

Solution 3 Link –

https://leetcode.com/problems/find-common-elements-between-two-
arrays/submissions/1374572685/

Time Complexity: O(n*m)

Space Complexity: O(1)

Screenshot:

Description:
Time Complexity: O(n*m)
Iterating to the entire n-sized array and again Iterating to the (m) sized array then perform
the operation that will be O(n*m)
This will perform 2 times
Total Time Complexity = O(n*m) + O(n*m) = O(n*m)
Space complexity: O(1)
No Extra space required for the Program Execution
Question 4 was https://leetcode.com/problems/number-of-good-pairs/

Solution 4 Link –

https://leetcode.com/problems/number-of-good-pairs/submissions/1374582852/

Time Complexity: O(n2)

Space Complexity: O(1)

Screenshot:

Description:
Time Complexity: O(n2)
Iterating to the entire n-sized array and again Iterating to the (n-i) sized array then
perform the operation that will be O(n*(n-1))

Space complexity: O(1)


No Extra space required for the Program Execution
Question 5 was https://leetcode.com/problems/shuffle-the-array/

Solution 5 Link –

https://leetcode.com/problems/shuffle-the-array/submissions/1374591325/

Time Complexity: O(n)

Space Complexity: O(n)

Screenshot:

Description:
Time Complexity: O(n)
Iterating to the entire n-sized array and for each array element performing O(1) operation
of push.

Space complexity: O(n)


The resultArray stores the results for each array element, so it will have the same length as the
nums array, which is n. so Space Complexity: O(n)

You might also like