0% found this document useful (0 votes)
16 views9 pages

21BCS7586 PriyanshuMalik Day 3

The document outlines a Java programming assignment for a student named Priyanshu Malik, detailing various coding tasks including removing duplicates from a sorted array, comparing version numbers, and calculating the maximum product of a subarray. It includes code snippets for each task, demonstrating different algorithms and data structures. The document appears to be part of a computer science curriculum focused on practical coding skills.

Uploaded by

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

21BCS7586 PriyanshuMalik Day 3

The document outlines a Java programming assignment for a student named Priyanshu Malik, detailing various coding tasks including removing duplicates from a sorted array, comparing version numbers, and calculating the maximum product of a subarray. It includes code snippets for each task, demonstrating different algorithms and data structures. The document appears to be part of a computer science curriculum focused on practical coding skills.

Uploaded by

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

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Day – 3 Java

Student Name: Priyanshu Malik UID: 21BCS7586


Branch: BE-CSE Section/Group: 21BCS_CC-652-A

Aim:
1) Metll Test Authenticate 1
2) Metll Test Authenticate 2
3) Remove Duplicates from Sorted Array
4) Compare Version Numbers
5) Add Binary
6) Zigzag Conversion
7) Permutations
8) Maximum Product Subarray e

Code and Output:


1)
public static int countUniqueDigits(String
number) {

HashSet<Character> uniqueDigits = new

HashSet<>(); for (char ch :

number.toCharArray()) {

if (Character.isDigit(ch)) { uniqueDigits.add(ch);
}
}
return uniqueDigits.size();
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
2) public static int countDigitsAppearingOnce(String number)
{
HashMap<Character, Integer> digitFrequency = new HashMap<>();

for (char ch : number.toCharArray()) {


if (Character.isDigit(ch)) {
digitFrequency.put(ch, digitFrequency.getOrDefault(ch, 0) + 1);
} } int count = 0;
for (int frequency : digitFrequency.values()) {
if (frequency == 1) {
count++;
} } return
count;
}

3) class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int j = 1;
for(int i = 1; i < nums.size(); i++){
if(nums[i] != nums[i - 1]){
nums[j] = nums[i];
j++;
}
} return
j;
}
};

4) class Solution {
public:
int compareVersion(string version1, string version2) {
int i = 0, j = 0;
int len1 = version1.length(), len2 = version2.length();
int num1 = 0, num2 = 0;
while (i < len1 or j < len2) {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
while (i < len1 and version1.at(i) != '.') num1 =
num1 * 10 + (version1.at(i++) - '0');
while (j < len2 and version2.at(j) != '.')
num2 = num2 * 10 + (version2.at(j++) - '0');

if (num1 > num2)


return 1;
else if (num1 < num2) return
-1;
num1 = 0, num2 = 0;
if (i < len1) i++;
if (j < len2)
j++
; } return
0;
}
};

5) class Solution { public:


string addBinary(string a, string b) {
string ans; int carry
= 0; int i =
a.length() - 1; int j
= b.length() - 1;
while (i >= 0 || j >= 0 || carry) {
if (i >= 0) carry +=
a[i--] - '0';
if (j >= 0)
carry += b[j--] - '0';
ans += carry % 2 + '0';
carry /= 2;
}

reverse(begin(ans), end(ans));
return ans;
} };
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
6) class Solution {
public:
string convert(string s, int numRows) {
if(numRows==1) return s;
vector<string> rows(min(numRows,
int(s.size())));
int curRow=0; bool
goingDown=false;
for(char c: s){
rows[curRow] += c; if(curRow==0 ||
curRow == numRows-1)
goingDown =! goingDown; curRow
+= goingDown?1: -1;
} string
ret;
for(string row: rows) ret += row;
return ret;

}
};

7)

class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if (nums.length == 1) {
List<Integer> singleList = new
ArrayList<>(); singleList.add(nums[0]);
res.add(singleList);
return res;
}

for (int i = 0; i < nums.length; i++) {


int n = nums[i];
int[] remainingNums = new int[nums.length - 1];
int index = 0;
for (int j = 0; j < nums.length; j++) {
if (j != i) {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
remainingNums[index] = nums[j];
index++;
}
}

List<List<Integer>> perms = permute(remainingNums);


for (List<Integer> p : perms) {
p.add(n);
}

res.addAll(perms);
}

return res;
}
}

8)
class Solution {
public int maxProduct(int[] nums) {
if (nums.length == 0) {
return 0;
}

int maxSoFar = nums[0]; int


minSoFar = nums[0]; int
result = maxSoFar;
for (int i = 1; i < nums.length; i++) { int
curr = nums[i];

int tempMaxSoFar = Math.max(curr, Math.max(maxSoFar * curr, minSoFar *


curr));

minSoFar = Math.min(curr, Math.min(maxSoFar * curr, minSoFar * curr));

maxSoFar = tempMaxSoFar;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
result = Math.max(maxSoFar, result);
}

return result;
}
}
Output
1.

2.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

3.

4.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

5.

6.

7.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

8.

You might also like