0% found this document useful (0 votes)
27 views

Problem Set of Leetcode Java

Problem of leeetcode

Uploaded by

Saurav Singha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Problem Set of Leetcode Java

Problem of leeetcode

Uploaded by

Saurav Singha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Winning Camp Worksheet (Java)


Day: 21
Name : Saurav Singha UID : 21BCS5421
Subject : Java Section : SC-904 (B)
Date : 21/06/2024

Problem 1: Duplicate Zeroes


Solution:
class Solution {
public void duplicateZeros(int[] arr) {
int[] tempArr = new int[arr.length];
for (int arrIndex = 0, tempIndex = 0; tempIndex < tempArr.length; arrIndex++)
{
tempArr[tempIndex] = arr[arrIndex];
if (tempArr[tempIndex++] == 0 && tempIndex < tempArr.length) {
tempArr[tempIndex++] = 0;
}
}
System.arraycopy(tempArr, 0, arr, 0, arr.length);
}
}
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Problem 2: Remove Linked List Elements


Solution:
class Solution {
public ListNode removeElements(ListNode head, int val) {
ListNode sentinel = new ListNode(0);
sentinel.next = head;
ListNode prev = sentinel;
while (head != null) {
if (head.val == val) {
prev.next = head.next;
} else {
prev = head;
}
head = head.next;
}
return sentinel.next;
}
}

class ListNode {
int val;
ListNode next;
ListNode(int val) { this.val = val; }
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Output:

Problem 3: Shortest Subarray with Sum at Least K


Solution:
Output:
import java.util.*;
class Solution {
public int shortestSubarray(int[] nums, int k) {
int n = nums.length;
Deque<Pair> dq = new ArrayDeque<>();
long sum = 0;
int shortest = Integer.MAX_VALUE;

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


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

sum += nums[i];
if (sum >= k) {
shortest = Math.min(shortest, i + 1);
}
while (!dq.isEmpty() && sum - dq.peekFirst().sum >= k) {
shortest = Math.min(shortest, i - dq.pollFirst().index);
}
while (!dq.isEmpty() && sum <= dq.peekLast().sum) {
dq.pollLast();
}
dq.addLast(new Pair(sum, i));
}

return shortest == Integer.MAX_VALUE ? -1 : shortest;


}
static class Pair {
long sum;
int index;
Pair(long sum, int index) {
this.sum = sum;
this.index = index;
}
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Output:

Problem 4: Multiply Strings


Solution:
class Solution {
public String multiply(String num1, String num2) {
int n1 = num1.length(), n2 = num2.length();
int[] products = new int[n1 + n2];
for (int i = n1 - 1; i >= 0; i--) {
for (int j = n2 - 1; j >= 0; j--) {
int d1 = num1.charAt(i) - '0';
int d2 = num2.charAt(j) - '0';
products[i + j + 1] += d1 * d2;
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

int carry = 0;
for (int i = products.length - 1; i >= 0; i--) {
int temp = (products[i] + carry) % 10;
carry = (products[i] + carry) / 10;
products[i] = temp;
}
StringBuilder sb = new StringBuilder();
for (int num : products) {
sb.append(num);
}
while (sb.length() != 0 && sb.charAt(0) == '0') {
sb.deleteCharAt(0);
}
return sb.length() == 0 ? "0" : sb.toString();
}
}
Output:

You might also like