0% found this document useful (0 votes)
2 views19 pages

Array Programs

The document contains Java code snippets for various array manipulation algorithms, including finding the largest and smallest elements, rotating arrays, moving zeros, removing duplicates, and calculating the maximum subarray sum. It also includes solutions for problems like finding missing numbers, two-sum, three-sum, and trapping rainwater. Each code snippet is structured as a separate public class with a main method demonstrating the respective algorithm.
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)
2 views19 pages

Array Programs

The document contains Java code snippets for various array manipulation algorithms, including finding the largest and smallest elements, rotating arrays, moving zeros, removing duplicates, and calculating the maximum subarray sum. It also includes solutions for problems like finding missing numbers, two-sum, three-sum, and trapping rainwater. Each code snippet is structured as a separate public class with a main method demonstrating the respective algorithm.
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

1.

Find the largest and LargestSmallest


public class LargestSmallest {
public static void main(String[] args) {
int[] arr = { 5, 2, 9, 1, 7 };
int max = arr[0];
int min = arr[0];
for (int n : arr) {
if (n > max) {
max = n;
}
if (n < min) {
min = n;
}
}
[Link]("Largest=" + max);
[Link]("Smallest=" + min);
}
[Link] the SecondLargest
public class SecondLargest {
public static void main(String[] args) {
int[] arr = { 10, 20, 4, 45, 99 };
int first = Integer.MIN_VALUE;
int second = Integer.MIN_VALUE;
for (int n : arr) {
if (n > first) {
second = first;
first = n;
} else if (n > second && n != first)
second = n;
}
[Link](second);
}
}
3️⃣ Check if Array is Sorted
package Arrays1;

public class CheckArraySorted {


public static void main(String[] args) {
int[] arr = { 1, 2, 3, 4, 5 };
boolean sorted = true;
for (int i = 1; i < [Link]; i++) {
if (arr[i] < arr[i - 1]) {
sorted = false;
break;
}
}
[Link](sorted ? "Sorted" : "Not Sorted");
}
}
[Link] Rotate Array by K
import [Link];
public class RotateArray {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int k = 2;
k = k % [Link];
int[] rotated = new int[[Link]];
for (int i = 0; i < [Link]; i++)
rotated[i] = arr[(i + k) % [Link]];
[Link]([Link](rotated));
}
}.
[Link] Zeros to end
package Arrays1;
import [Link];
public class MoveZeros {

public static void main(String[] args) {

int[] arr={0,1,0,3,12};

int index=0;

for(int i=0;i<[Link];i++){

if(arr[i]!=0)
arr[index++]=arr[i];
}
while(index<[Link])
arr[index++]=0;
for(int num:arr)
[Link](num+" ");
}
}
[Link] Duplicated from Sorted array
public class RemoveDuplicates {
public static void main(String[] args) {
int[] arr={1,1,2,2,3,4,4};
int j=0;
for(int i=1;i<[Link];i++){
if(arr[i]!=arr[j]){
j++;
arr[j]=arr[i];
}
}
for(int i=0;i<=j;i++)
[Link](arr[i]+" ");
}
}
[Link] the Missing Number from given Array
public class MissingNumber {
public static void main(String[] args) {
int[] arr = { 1, 2, 4, 5 };
int n = [Link]+1;
int totalsum = n*(n + 1) / 2;
int sum=0;
for(int i=0;i<[Link];i++)
{
sum+=arr[i];
}
[Link](totalsum-sum);
}
}
[Link]
public class TwoSum {
public static void main(String[] args) {
int[] arr={2,7,11,15};
int target=9;
for(int i=0;i<[Link];i++){
for(int j=i+1;j<[Link];j++){
if(arr[i]+arr[j]==target){
[Link](i+" "+j);
}
}
}
}
}
[Link]
public class ThreeSum {

public static void main(String[] args) {

int[] arr={1,2,3,4,5};

int target=9;

for(int i=0;i<[Link];i++){

for(int j=i+1;j<[Link];j++){

for(int k=j+1;k<[Link];k++){

if(arr[i]+arr[j]+arr[k]==target){

[Link](arr[i]+" "+arr[j]+"
"+arr[k]);
}
}
}
}
}
}
[Link] SubArray Sum
public class Kadane {

public static void main(String[] args) {

int[] arr={-2,1,-3,4,-1,2,1,-5,4};

int max=arr[0];

int current=arr[0];

for(int i=1;i<[Link];i++){

current=[Link](arr[i],current+arr[i]);

max=[Link](max,current);
}

[Link]("Maximum Sum = "+max);


}
}
[Link] of ArrayExcept Self
public class ProductExceptSelf {
public static void main(String[] args) {

int[] arr={1,2,3,4};

int n=[Link];

int[] result=new int[n];

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

int product=1;

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

if(i!=j)
product*=arr[j];
}

result[i]=product;
}

for(int num:result)
[Link](num+" ");
}}
[Link] duplicate Elements in the given Array
public class DuplicateElement {

public static void main(String[] args) {

int[] arr={1,2,3,4,2};

for(int i=0;i<[Link];i++){

for(int j=i+1;j<[Link];j++){

if(arr[i]==arr[j]){

[Link]("Duplicate = "+arr[i]);
}
}
}
}
}
[Link] Two Sorted Arrays
import [Link];
public class MergeSortedArrays {
public static void main(String[] args) {
int[] arr1 = {1, 3, 5, 7};
int[] arr2 = {2, 4, 6, 8};
int[] result = new int[[Link] + [Link]];
int i = 0, j = 0, k = 0;
while (i < [Link] && j < [Link]) {
if (arr1[i] < arr2[j]) {
result[k] = arr1[i];
i++;
} else {
result[k] = arr2[j];
j++;
}
k++;
}
while (i < [Link]) {
result[k] = arr1[i];
i++;
k++;
}
while (j < [Link]) {
result[k] = arr2[j];
j++;
k++;
}
[Link]("Merged Array:");
[Link]([Link](result));
}
}
[Link] with Most Water
Given an array height[], where each element represents the height of a vertical
line, find the maximum amount of water that can be contained between any
two lines.

public class ContainerWithMostWater {


public static void main(String[] args) {
int[] height = {1, 8, 6, 2, 5, 4, 8, 3, 7};
int left = 0;
int right = [Link] - 1;
int maxArea = 0;
while (left < right) {
int width = right - left;
int minHeight = [Link](height[left],height[right]);
int area = width * minHeight;
if (area > maxArea)
{
maxArea = area;
}

if (height[left] < height[right]) {


left++;
} else {
right--;
}
}

[Link]("Maximum Water = " + maxArea);


}
}
[Link] Rain Water
Given an array height[] representing the elevation map, calculate how much
rainwater can be trapped after raining.

public class TrappingRainWater {

public static void main(String[] args) {

int[] height = {4, 2, 0, 3, 2, 5};

int n = [Link];

int[] leftMax = new int[n];


int[] rightMax = new int[n];

// Fill leftMax array


leftMax[0] = height[0];
for (int i = 1; i < n; i++) {
leftMax[i] = [Link](leftMax[i - 1], height[i]);
}
// Fill rightMax array
rightMax[n - 1] = height[n - 1];
for (int i = n - 2; i >= 0; i--) {
rightMax[i] = [Link](rightMax[i + 1],
height[i]);
}

int water = 0;

// Calculate trapped water


for (int i = 0; i < n; i++) {
water += [Link](leftMax[i], rightMax[i]) -
height[i];
}

[Link]("Total Water Trapped = " +


water);
}
}

You might also like