YR AP 1.4
YR AP 1.4
YR AP 1.4
Experiment - 1.4
Student Name: Yash Raj UID: 21BCS11765
Branch: BE-CSE Section/Group: 649 ‘A’
Date of Performance: 7th Feb 2024 Semester: 6
Subject Name: Advanced Programming Lab - II Subject Code: 21CSP-351
1. Aim:
• To Solve Missing Number.
• To Solve longest duplicate substring.
2. Objective:
Problem 1: Algorithm:
1. Initialize a variable sum to 0. This will be used to calculate the sum of all
elements in the given vector nums.
2. Obtain the size of the vector nums and store it in the variable n.
3. Calculate the expected total sum of numbers from 0 to n using the formula
totalSum = (n*(n+1))/2.
4. Iterate over each element of the vector nums:
Add the value of the current element to the variable sum.
5. After the loop, return the difference between the expected total sum
(totalSum) and the actual sum of elements (sum). This difference
represents the missing number in the sequence from 0 to n.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Code:
class Solution {
public int missingNumber(int[] nums) {
int n = nums.length;
int[] v = new int[n+1];
Arrays.fill(v, -1);
for(int i = 0; i < nums.length; i++) {
v[nums[i]] = nums[i];
}
for(int i = 0; i < v.length; i++) {
if(v[i] == -1) return i;
}
return 0;
}
}
OUTPUT:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Problem 2:
Algorithm:
1. Initialization:
• Initialize variables n as the size of the input string s, l as 1
(minimum length of the duplicated substring), and r as n-1
(maximum length of the duplicated substring).
• Initialize a prime number prime (ideally a large prime number) and
compute powers of 26 modulo prime up to the length of the input
string. These powers will be used in the Rabin-Karp algorithm for
rolling hash computation.
2. Binary Search for Length of Duplicated Substring:
• Perform a binary search to find the longest duplicated substring
length within the given range [l, r].
• In each iteration, calculate the middle length mid.
• Apply the Rabin-Karp algorithm to find a duplicated substring of
length mid.
• If a duplicated substring of length mid is found, update the result
and move the left boundary l to mid + 1 to search for longer
duplicated substrings.
• Otherwise, move the right boundary r to mid - 1.
3. Rabin-Karp Algorithm:
• The rabinKarp function takes the input string s and the length of
the duplicated substring len.
• Initialize a rolling hash curr and an unordered map hash to store
hashes and their corresponding positions.
• Compute the initial hash for the first len characters of the input
string.
• Iterate through the input string starting from index len.
• Update the rolling hash using the Rabin-Karp rolling hash update
formula.
• Check if the current hash is already present in the hash map. If not,
store it along with its position.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
• If the hash is already present, compare the substring with the
substring at the stored position. If they match, return the duplicated
substring.
• If no duplicated substring is found, return an empty string.
Code:
class Solution {
String str;
int len;
String r = "";
int s = 0;
int e = S.length();
while (s < e) {
int m = (s+e)/2;
String tmp = checkDup(m);
if (tmp != null) {
r = tmp;
s = m+1;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
} else {
e = m;
}
}
return r;
}
}
OUTPUT:
DEPARTMENT OF
Learning Outcomes: