Ap Worksheet 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Experiment1.

Student Name:Aaditya Narain Singh UID: 21BCS3653


Branch:BE-CSE Section/Group:CC-649
Semester: 6 Date of Performance:13-01-2024
Subject Name: Advance Programming lab
Subject Code:21CSP-251

1. Aim:
• To Solve the Jump Game 2
• To Solve the 3 SUM Problem

2. Objective:
• You are given a 0-indexed array of integers nums of length n. You are initially
positioned at nums[0].
• Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that
i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.

3. Algo. /Approach and output:


class Solution:
def jump(self, a):
jump, currEnd, nextEnd = 0, 0, 0
for i in range(len(a)-1):
nextEnd = max(nextEnd, i + a[i])
if currEnd == i:
currEnd = nextEnd
jump +=1
return jump
class Solution:
def threeSum(self, nums):
nums.sort()
triplets = set()
for i in range(len(nums) - 2):
firstNum = nums[i]
j = i + 1
k = len(nums) - 1
while j < k:
secondNum = nums[j]
thirdNum = nums[k]
potentialSum = firstNum + secondNum + thirdNum
if potentialSum > 0:
k -= 1
elif potentialSum < 0:
j += 1
else:
triplets.add((firstNum , secondNum ,thirdNum))
j += 1
k -= 1
return triplets

You might also like