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

Assignment

Uploaded by

ANITHARANI K
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Assignment

Uploaded by

ANITHARANI K
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Anitharani K

1. Given an array which consists of only 0, 1 and 2. Sort the array without using any
algorithm
public class AR {
public static void main(String[] args) {
int[] arr = {0,1,2,0,1,2,0};
int count0 = 0, count1 = 0, count2 = 0;
for (int num : arr) {
if (num == 0)
count0++;
else if (num == 1)
count1++;
else if (num == 2)
count2++;
}
int index = 0;
for (int i = 0; i < count0; i++)
arr[index++] = 0;
for (int i = 0; i < count1; i++)
arr[index++] = 1;
for (int i = 0; i < count2; i++)
arr[index++] = 2;
for (int num : arr) {
System.out.print(num + " ");
}
}
}
output:

2. Find the “Kth” max and min element of an array


import java.util.Arrays;

public class AR {
public static void main(String[] args) {
int[] arr = {3, 2, 4, 1, 3, 7, 0, 5, 9};
int k = 1;
Arrays.sort(arr);
Anitharani K

if (k < 1 || k > arr.length) {


System.out.println("Invalid value of k.");
return;
}

int kthMin = arr[k - 1];


int kthMax = arr[arr.length - k];
System.out.println("Kth smallest element: " + kthMin);
System.out.println("Kth largest element: " + kthMax);
}
}

output:

3. Move all the negative elements to one side of the array

import java.util.Arrays;

public class AR {
public static void main(String[] args) {
int[] arr = {-22, 10, -3, -1, 2, -4, 9, -5, -7};
int n = arr.length;
int left = 0, right = n - 1;
while (left <= right) {
if (arr[left] < 0) {
left++;
}
else if (arr[right] >= 0) {
right--;
}
else {
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
Anitharani K

}
for (int num : arr) {
System.out.print(num + " ");
}
}
}

output:

4. Find the Union and Intersection of the two sorted arrays.

public class AR {
public static void main(String[] args) {
int[] arr1 = {1, 3, 4, 5, 7};
int[] arr2 = {2, 3, 5, 6};

System.out.println("Union of the arrays:");


printUnion(arr1, arr2);

System.out.println("\nIntersection of the arrays:");


printIntersection(arr1, arr2);
}

// Function to print union of arr1 and arr2


public static void printUnion(int[] arr1, int[] arr2) {
int m = arr1.length;
int n = arr2.length;
int i = 0, j = 0;

for (; i < m && j < n; ) {


if (arr1[i] < arr2[j]) {
System.out.print(arr1[i++] + " ");
} else if (arr2[j] < arr1[i]) {
System.out.print(arr2[j++] + " ");
} else {
System.out.print(arr2[j++] + " ");
Anitharani K

i++;
}
}

for (; i < m; i++) {


System.out.print(arr1[i] + " ");
}

for (; j < n; j++) {


System.out.print(arr2[j] + " ");
}
}

// Function to print intersection of arr1 and arr2


public static void printIntersection(int[] arr1, int[] arr2) {
int m = arr1.length;
int n = arr2.length;
int i = 0, j = 0;

for (; i < m && j < n; ) {


if (arr1[i] < arr2[j]) {
i++;
} else if (arr2[j] < arr1[i]) {
j++;
} else {
System.out.print(arr2[j++] + " ");
i++;
}
}
}
}

output:
Anitharani K

5. Find Largest sum contiguous Subarray


public class AR {
public static void main(String[] args) {
int[] arr = {-2, -3, 4, -1, -2, 1, 5, -3};
int maxSum = kadane(arr);
System.out.println("Largest sum contiguous subarray: " + maxSum);
}
public static int kadane(int[] arr) {
int maxEndingHere = arr[0];
int maxSoFar = arr[0];
for (int i = 1; i < arr.length; i++) {
maxEndingHere = Math.max(arr[i], maxEndingHere + arr[i]);
maxSoFar = Math.max(maxSoFar, maxEndingHere);
}
return maxSoFar;
}
}
output:

5. Minimize the maximum difference between heights

import java.util.*;
public class AR {
public static void main(String[] args) {

int[] arr = {1,4,8,9,5};


int k = 3;
int result = minimizeMaxDifference(arr, k);
System.out.println("Minimum maximum difference between heights: " + result);
}
public static int minimizeMaxDifference(int[] arr, int k) {
int n = arr.length;
Arrays.sort(arr);
int result = arr[n - 1] - arr[0];
Anitharani K

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


int minElement = Math.min(arr[0] + k, arr[i] - k);
int maxElement = Math.max(arr[n - 1] - k, arr[i - 1] + k);
result = Math.min(result, maxElement - minElement);
}
return result;
}
}
output:

7. Minimum no. of Jumps to reach the end of an array


import java.util.*;

public class AR {
public static void main(String[] args) {
int[] arr = {2, 3, 1, 1, 2, 4, 2, 0, 1, 1};
int minJumps = minJumpsToEnd(arr);
System.out.println("Minimum number of jumps: " + minJumps);
}

public static int minJumpsToEnd(int[] arr) {


int n = arr.length;
if (n <= 1) {
return 0;
}
int maxReach = arr[0];
int steps = arr[0];
int jumps = 1;
for (int i = 1; i < n; i++) {
if (i == n - 1) {
return jumps;
}
maxReach = Math.max(maxReach, i + arr[i]);
steps--;
if (steps == 0) {
jumps++;
if (i >= maxReach) {
return -1;
Anitharani K

}
steps = maxReach - i;
}
}
return -1;
}
}

output:

8. Find duplicate in an array of N+1 Integers


import java.util.*;

public class AR {
public static void main(String[] args) {
int[] nums = {1, 3, 4, 2, 2};
int duplicate = findDuplicate(nums);
System.out.println("Duplicate number: " + duplicate);
}
public static int findDuplicate(int[] nums) {
int x = nums[0];
int hare = nums[0];
do {
x = nums[x];
hare = nums[nums[hare]];
} while (x != hare);
x = nums[0];
while (x != hare) {
x = nums[x];
hare = nums[hare];
}
return x;
}
}

output:
Anitharani K

9. Merge 2 sorted arrays without using extra space


public class AR {
public static void main(String[] args) {
int[] nums1 = {1, 3, 5, 7, 0, 0, 0};
int m = 4;
int[] nums2 = {2, 4, 6};
int n = 3;
merge(nums1, m, nums2, n);
for (int num : nums1) {
System.out.print(num + " ");
}
}
public static void merge(int[] nums1, int m, int[] nums2, int n) {
int i = m - 1;
int j = n - 1; int k = m + n - 1;
while (i >= 0 && j >= 0) {
if (nums1[i] > nums2[j]) {
nums1[k--] = nums1[i--];
} else {
nums1[k--] = nums2[j--];
}
}
while (j >= 0) {
nums1[k--] = nums2[j--];
}
}
}
output:
Anitharani K

10. Kadane’s Algorithm

import java.util.*;

public class AR {
public static void main(String[] args) {
int[] arr = {-2, -3, 4, -1, -2, 1, 5, -3};

int maxEndingHere = 0;
int maxSoFar = Integer.MIN_VALUE;

for (int num : arr) {


maxEndingHere = Math.max(num, maxEndingHere + num);
maxSoFar = Math.max(maxSoFar, maxEndingHere);
}

System.out.println("Maximum sum contiguous subarray: " + maxSoFar);


}
}

output:

You might also like