0% found this document useful (0 votes)
1 views14 pages

Java Programs

The document contains Java code snippets for various array-related problems, including finding the largest and smallest elements, the second largest element, checking if an array is sorted, rotating an array, moving zeros to the end, removing duplicates, finding a missing number, and solving the TwoSum and ThreeSum problems. Additionally, it includes implementations for calculating the maximum subarray sum and the product of an array except itself. Each code snippet is structured as a separate public class with a main method demonstrating the solution to the respective problem.
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)
1 views14 pages

Java Programs

The document contains Java code snippets for various array-related problems, including finding the largest and smallest elements, the second largest element, checking if an array is sorted, rotating an array, moving zeros to the end, removing duplicates, finding a missing number, and solving the TwoSum and ThreeSum problems. Additionally, it includes implementations for calculating the maximum subarray sum and the product of an array except itself. Each code snippet is structured as a separate public class with a main method demonstrating the solution to the respective problem.
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]);
}
}
}
}
}

You might also like