0% found this document useful (0 votes)
13 views11 pages

Dsa

Uploaded by

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

Dsa

Uploaded by

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

NAME - Tanmoyee Sikder

PRN -240843120094

1.

package assignmentdsa;
import java.util.Arrays;

public class ThreeSumClosest {


public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int n = nums.length;
int closestSum = nums[0] + nums[1] + nums[2];

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


int left = i + 1,right = n - 1;
while (left < right) {
int currentSum = nums[i] + nums[left] + nums[right];
if (Math.abs(currentSum - target) < Math.abs(closestSum - target))
{
closestSum = currentSum;

}
if (currentSum < target) {
left++;
} else {
right--;
}
}
}

return closestSum;

}
}

package assignmentdsa;

public class useroftarget


{
public static void main(String[] args)
{
int[] arr= {-1,2,1,-4};
ThreeSumClosest sc=new ThreeSumClosest();
int result = sc.threeSumClosest(arr,1);
System.out.println("The sum of the three numbers closest to the
target is: " + result);
}

-----------------------------------------------------------------------------------
------------------------

2.

public class FindMissingPositive {


public static int findMissingPositive(int[] nums) {
int n = nums.length;

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

while (nums[i] > 0 && nums[i] <= n && nums[i] != nums[nums[i] - 1]) {

int temp = nums[i];


nums[i] = nums[temp - 1];
nums[temp - 1] = temp;
}
}

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


if (nums[i] != i + 1) {
return i + 1;
}
}

return n + 1;
}

public static void main(String[] args) {


int[] nums = {1,2,0};
int missing = findMissingPositive(nums);
System.out.println("The smallest missing positive integer is: " + missing);
}
}

-----------------------------------------------------------------------------------
------------------------------

3.

public class SortedSquares {


public static int[] sortedSquares(int[] nums) {
int n = nums.length;
int[] squares = new int[n];

int left = 0, right = n - 1, i = n - 1;


while (left <= right) {
int leftSquare = nums[left] * nums[left];
int rightSquare = nums[right] * nums[right];

if (leftSquare >= rightSquare)


{
squares[i--] = leftSquare;
left++;
} else {
squares[i--] = rightSquare;
right--;
}
}

return squares;
}

public static void main(String[] args)


{
int[] nums = {-4, -1, 0, 3, 10};
int[] squares = sortedSquares(nums);
System.out.println("Squares of the sorted array: ");
for (int num : squares) {
System.out.print(num + " ");
}
}
}

-----------------------------------------------------------------------------------
---------------
4.

public class SmallerNumbersThanCurrent {


public static int[] smallerNumbersThanCurrent(int[] nums) {
int[] sortedNums = nums.clone();
Arrays.sort(sortedNums);

int[] result = new int[nums.length];


for (int i = 0; i < nums.length; i++) {
int index = Arrays.binarySearch(sortedNums, nums[i]);
result[i] = index >= 0 ? index : -index - 1;
}

return result;
}

public static void main(String[] args) {


int[] nums = {8, 1, 2, 2, 3};
int[] smallerCounts = smallerNumbersThanCurrent(nums);
System.out.println(Arrays.toString(smallerCounts));
}
}

-----------------------------------------------------------------------------------
-----------------------
5.

public class AverageSalaryExcludingMinAndMax {


public double average(int[] salary) {
int minSalary = Integer.MAX_VALUE;
int maxSalary = Integer.MIN_VALUE;
int sum = 0;

for (int sal : salary) {


sum += sal;
minSalary = Math.min(minSalary, sal);
maxSalary = Math.max(maxSalary, sal);
}

return (double)(sum - minSalary - maxSalary) / (salary.length - 2);


}

public static void main(String[] args) {


int[] salary = {4000, 3000, 1000, 2000};
AverageSalaryExcludingMinAndMax solution = new
AverageSalaryExcludingMinAndMax();
double averageSalary = solution.average(salary);
System.out.println("Average salary excluding min and max: " +
averageSalary);
}
}

-----------------------------------------------------------------------------------
------------------------
6.

package assignmentdsa;
import java.util.Arrays;

/*public class ThreeSumClosest {


public int threeSumClosest(int[] nums, int target) {
Arrays.sort(nums);
int n = nums.length;
int closestSum = nums[0] + nums[1] + nums[2];

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


int left = i + 1,right = n - 1;
while (left < right) {
int currentSum = nums[i] + nums[left] + nums[right];
if (Math.abs(currentSum - target) < Math.abs(closestSum - target))
{
closestSum = currentSum;

}
if (currentSum < target) {
left++;
} else {
right--;
}
}
}

return closestSum;

}
}

*/

//public class FindMissingPositive {


// public static int findMissingPositive(int[] nums) {
// int n = nums.length;
//
//
// for (int i = 0; i < n; i++) {
//
// while (nums[i] > 0 && nums[i] <= n && nums[i] != nums[nums[i] - 1]) {
//
// int temp = nums[i];
// nums[i] = nums[temp - 1];
// nums[temp - 1] = temp;
// }
// }
//
// for (int i = 0; i < n; i++) {
// if (nums[i] != i + 1) {
// return i + 1;
// }
// }
//
// return n + 1;
// }
//
// public static void main(String[] args) {
// int[] nums = {1,2,0};
// int missing = findMissingPositive(nums);
// System.out.println("The smallest missing positive integer is: " +
missing);
// }
//}

//public class SortedSquares {


// public static int[] sortedSquares(int[] nums) {
// int n = nums.length;
// int[] squares = new int[n];
//
//
// int left = 0, right = n - 1, i = n - 1;
// while (left <= right) {
// int leftSquare = nums[left] * nums[left];
// int rightSquare = nums[right] * nums[right];
//
// if (leftSquare >= rightSquare)
// {
// squares[i--] = leftSquare;
// left++;
// } else {
// squares[i--] = rightSquare;
// right--;
// }
// }
//
// return squares;
// }
//
// public static void main(String[] args)
// {
// int[] nums = {-4, -1, 0, 3, 10};
// int[] squares = sortedSquares(nums);
// System.out.println("Squares of the sorted array: ");
// for (int num : squares) {
// System.out.print(num + " ");
// }
// }
//}

//public class SmallerNumbersThanCurrent {


// public static int[] smallerNumbersThanCurrent(int[] nums) {
// int[] sortedNums = nums.clone();
// Arrays.sort(sortedNums);
//
// int[] result = new int[nums.length];
// for (int i = 0; i < nums.length; i++) {
// int index = Arrays.binarySearch(sortedNums, nums[i]);
// result[i] = index >= 0 ? index : -index - 1;
// }
//
// return result;
// }
//
// public static void main(String[] args) {
// int[] nums = {8, 1, 2, 2, 3};
// int[] smallerCounts = smallerNumbersThanCurrent(nums);
// System.out.println(Arrays.toString(smallerCounts)); // Output: [4, 0, 1,
1, 2]
// }
//}

//public class AverageSalaryExcludingMinAndMax {


// public double average(int[] salary) {
// int minSalary = Integer.MAX_VALUE;
// int maxSalary = Integer.MIN_VALUE;
// int sum = 0;
//
// for (int sal : salary) {
// sum += sal;
// minSalary = Math.min(minSalary, sal);
// maxSalary = Math.max(maxSalary, sal);
// }
//
// return (double)(sum - minSalary - maxSalary) / (salary.length - 2);
// }
//
// public static void main(String[] args) {
// int[] salary = {4000, 3000, 1000, 2000};
// AverageSalaryExcludingMinAndMax solution = new
AverageSalaryExcludingMinAndMax();
// double averageSalary = solution.average(salary);
// System.out.println("Average salary excluding min and max: " +
averageSalary);
// }
//}

import java.util.PriorityQueue;

public class KthLargestElement {


public int findKthLargest(int[] nums, int k) {

PriorityQueue<Integer> minHeap = new PriorityQueue<>();

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


minHeap.offer(nums[i]);
}

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

if (nums[i] > minHeap.peek()) {

minHeap.poll();
minHeap.offer(nums[i]);
}
}

return minHeap.peek();
}

public static void main(String[] args) {


int[] nums = {3, 2, 1, 5, 6, 4};
int k = 2;
KthLargestElement solution = new KthLargestElement();
int kthLargest = solution.findKthLargest(nums, k);
System.out.println("The " + k + "th largest element is: " + kthLargest);

}
}

-----------------------------------------------------------------------------------
-----------------------

7.

package assignmentdsa;

public class RotateImage {


public void rotate(int[][] matrix) {
int n = matrix.length;

// Transpose the matrix


for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}

// Reverse each row


for (int i = 0; i < n; i++) {
for (int j = 0; j < n / 2; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[i][n - j - 1];
matrix[i][n - j - 1] = temp;

}
}
}

public static void main(String[] args) {


int[][] matrix = {{1, 2, 3,4}, {5, 6,7,8}, {9,10,11,12},{13,14,15,16}};
RotateImage solution = new RotateImage();
solution.rotate(matrix);

// Print the rotated matrix


for (int[] row : matrix) {
for (int num : row) {
System.out.print(num + " ");
}
System.out.println();
}
}
}

-----------------------------------------------------------------------------------
-------------------------
8.

package assignmentdsa;

public class MergeTwoSortedLists {


public static class ListNode {
int val;
ListNode next;

ListNode(int x) {
val = x;
next = null;
}
}

public ListNode mergeTwoLists(ListNode l1, ListNode l2) {


if (l1 == null) {
return l2;
}
if (l2 == null) {
return l1;
}

ListNode dummyHead = new ListNode(0);


ListNode tail = dummyHead;

while (l1 != null && l2 != null) {


if (l1.val <= l2.val) {
tail.next = l1;
l1 = l1.next;
} else {
tail.next = l2;
l2 = l2.next;
}
tail = tail.next;
}

if (l1 != null) {
tail.next = l1;
} else {
tail.next = l2;
}
return dummyHead.next;
}

public static void main(String[] args) {

ListNode head1 = new ListNode(5);


head1.next = new ListNode(10);
head1.next.next = new ListNode(15);

ListNode head2 = new ListNode(2);


head2.next = new ListNode(3);
head2.next.next = new ListNode(20);

MergeTwoSortedLists solution = new MergeTwoSortedLists();


ListNode mergedHead = solution.mergeTwoLists(head1, head2);

while (mergedHead != null) {


System.out.print(mergedHead.val + " ");
mergedHead = mergedHead.next;
}
}
}

-----------------------------------------------------------------------------------
-----------------------------------------------
9.

class Node {
int data;
Node next;

public Node(int data) {


this.data = data;
this.next = null;
}
}

// LinkedList class with methods to add nodes, merge and display


class LinkedList {
Node head;

// Method to add nodes to the linked list


public void addNode(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
Node last = head;
while (last.next != null) {
last = last.next;
}
last.next = newNode;
}
}

// Method to merge two sorted linked lists


public static LinkedList merge(LinkedList list1, LinkedList list2) {
LinkedList mergedList = new LinkedList();
Node node1 = list1.head;
Node node2 = list2.head;

while (node1 != null && node2 != null) {


if (node1.data < node2.data) {
mergedList.addNode(node1.data);
node1 = node1.next;
} else if (node2.data < node1.data) {
mergedList.addNode(node2.data);
node2 = node2.next;
} else { // Handle duplicates
mergedList.addNode(node1.data);
node1 = node1.next;
node2 = node2.next;
}
}

// Add remaining nodes, if any


while (node1 != null) {
mergedList.addNode(node1.data);
node1 = node1.next;
}
while (node2 != null) {
mergedList.addNode(node2.data);
node2 = node2.next;
}

return mergedList;
}

// Method to display linked list


public void display() {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
}

public class Main {


public static void main(String[] args) {
LinkedList list1 = new LinkedList();
list1.addNode(1);
list1.addNode(1);
list1.addNode(4);
list1.addNode(5);
list1.addNode(7);

LinkedList list2 = new LinkedList();


list2.addNode(2);
list2.addNode(4);
list2.addNode(7);
list2.addNode(9);

System.out.println("List 1:");
list1.display();
System.out.println("List 2:");
list2.display();

LinkedList mergedList = LinkedList.merge(list1, list2);


System.out.println("Merged List:");
mergedList.display();
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy