Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

YR AP 1.4

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

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

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:

1) Problem statement - Given an array nums containing n distinct numbers in


the range [0, n], return the only number in the range that is missing from the
array.

2) Problem Statement - Given a string s, consider all duplicated substrings:


(contiguous) substrings of s that occur 2 or more times. The occurrences
may overlap.
Return any duplicated substring that has the longest possible length. If s
does not have a duplicated substring, the answer is "".

3. Algorithm, Script and Output:

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

Time and Space Complexity:


The time complexity of this program is O(N).
The space complexity is O(1).

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;

private class SubStr {


private int hash;
private int off;

public SubStr(int off, int hash) {


this.off = off;
this.hash = hash;
//System.out.println(off+" "+hash);
}

public int hashCode() {


return this.hash;
}

public boolean equals(Object o) {


SubStr x = (SubStr)o;
if (x.off == this.off) {
return true;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
if (x.hash != this.hash) {
return false;
}
for (int i=0; i<len; i++) {
if (str.charAt(x.off+i) != str.charAt(this.off+i)) {
return false;
}
}
return true;
}

public int getOffset() {


return this.off;
}

public String toString() {


return str.substring(this.off, this.off+len);
}
}

private String checkDup(int len) {


this.len = len;
HashSet<SubStr> s = new HashSet();
int hash = 0;
int p = 1;

for (int i=0; i<len; i++) {


p = p * 33;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
hash = hash * 33 + str.charAt(i);
}
s.add(new SubStr(0, hash));
for (int i=len; i<str.length(); i++) {
hash = hash * 33 - str.charAt(i-len)*p + str.charAt(i);
SubStr ss = new SubStr(i-len+1, hash);
if (s.contains(ss)) {
return ss.toString();
} else {
s.add(ss);
}
}
return null;
}

public String longestDupSubstring(String S) {


this.str = S;
//System.out.println(checkDup(3));

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

COMPUTER SCIENCE & ENGINEERING

Time and Space complexity:

• The time complexity of the code O(n log n)


• Space complexity of the code is O(n)

Learning Outcomes:

1. I understood using math tricks helps make programs faster.


2. I understood breaking big problems into smaller steps can make them
easier to solve
3. I learnt choosing the right tools and organizing data smartly can make
programs work better.

You might also like