You are on page 1of 3

Experiment1.

Student Name:Shashi Ranjan Mehta UID: 21BCS7093


Branch:BE-CSE Section/Group:CC-FL-601
Semester: 6 Date of Performance:12-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 {

public:
int jump(vector<int>& nums) {
for(int i=1;i<nums.size();i++){
nums[i]=max(nums[i]+1,nums[i-1]);
}
int ant=0;
int ans=0;
while(ant<nums.size()-1){
ans++;
ant=nums[ant];
}
return ans;
}
};
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
int n=nums.size();
sort(nums.begin(),nums.end());
set<vector<int>>set;
vector<vector<int>>output;
for(int i=0;i<n-1;i++){
int low =i+1,high=n-1;
while(low<high){
if(nums[i]+nums[low]+nums[high]<0){
low++;
}
else if(nums[i]+nums[low]+nums[high]>0){
high--;
}
else{
output.push_back({nums[i],nums[low],nums[high]});
int tempIndex1=low,tempIndex2=high;
while(low<high&&nums[low]==nums[tempIndex1]) low++;
while(low<high && nums[high]==nums[tempIndex2]) high --;
}

}
while(i+1<n&& nums[i]==nums[i+1])i++;
}

return output;
}
};

You might also like