0% found this document useful (0 votes)
14 views5 pages

Ap1.1 Doc

Uploaded by

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

Ap1.1 Doc

Uploaded by

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

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING


Experiment1.1
Student Name: Pushpendra Kumar UID: 22BCS80081
Branch: BE.CSE Section/Group: SN-902/A
Semester: 6th Date of Performance:
Subject Name: Ap-Lab Subject Code: 21CSP-351
1.1
Question -1.
1. Aim: to find all unique triplets in a given list (nums) that sum up
to zero. The function three_sum uses a brute-force approach with
three nested loops to iterate through all possible combinations of
triplets and checks if their sum equals zero. The found triplets
are then added to the result list, which is returned at the end.

2. Objective: The objective of this code is to solve the 3Sum


problem. The 3Sum problem involves finding all unique triplets
in a given array of numbers such that the sum of the three
elements in the triplet equals zero. The code aims to use a
bruteforce approach to identify and return these triplets from the
input array.

3. Code :
class Solution { public:
vector<vector<int>> threeSum(vector<int>& nums)
{ sort(nums.begin(),nums.end());
int size = nums.size();
vector<vector<int>> temp; set<vector<int>>
DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING


ans; for(int i = 0 ; i<size; i++){ int j =
i+1 , k = size-1; while(j<k){ int
sum = nums[i] + nums[j] + nums[k];
if( sum== 0)
{ ans.insert({nums[i],nums[j],nums[k]}
); j++; k--;
}
else if(sum < 0) j++;
else k--;

}
}
for(auto i : ans)
{ temp.push_back(i);
}
return temp;
}
}
4. output:
DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Question-2

1. AIM: The aim of this Program is to determine whether it is


possible to reach the last index of an array (represented by the
nums parameter) using the values in the array as jump
DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING


lengths. The code employs a greedy strategy, updating the
maximum reachable position as it iterates through the array.
If, at any point, the current index is unreachable based on the
current maximum reach, the function returns False.
Otherwise, if the last index is reachable, it returns True.

2. Objective: The objective of this program is to solve the


"Jump Game" problem, which is to determine whether it is
possible to reach the last index of an array using the values in
the array as jump lengths. The program uses a greedy
approach to efficiently check if it's possible to reach the end
by iteratively updating the maximum reachable position. The
ultimate goal is to return True if reaching the last index is
possible and False otherwise.

3. Code:
class Solution { public: int
jump(vector<int>& nums) {

int n=nums.size();
if(n==1) return 0; int
curR=0; int maxR=0;
int ans=0;

for(int i=0;i<n-1;i++)
{ curR=max(curR,i+nums[i
]); if(i==maxR)
{ ans++;
maxR=curR;
DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING


}

}
return ans;

}
};
4.Output:

You might also like