0% found this document useful (0 votes)
31 views5 pages

Array

The document contains ten Java programs that perform various array operations. These include finding the second largest element, checking if an array is sorted, removing duplicates from a sorted array, left rotating an array, identifying leaders in an array, counting element frequencies, calculating the maximum subarray sum using Kadane's algorithm, moving zeros to the end of an array, finding a missing number, and identifying duplicates. Each program utilizes standard input for array elements and outputs the result of the operation.

Uploaded by

vasudevakakanha
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)
31 views5 pages

Array

The document contains ten Java programs that perform various array operations. These include finding the second largest element, checking if an array is sorted, removing duplicates from a sorted array, left rotating an array, identifying leaders in an array, counting element frequencies, calculating the maximum subarray sum using Kadane's algorithm, moving zeros to the end of an array, finding a missing number, and identifying duplicates. Each program utilizes standard input for array elements and outputs the result of the operation.

Uploaded by

vasudevakakanha
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
You are on page 1/ 5

1.

Find Second Largest Element

import java.util.Scanner;

public class SecondLargest {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) arr[i] = sc.nextInt();
int first = Integer.MIN_VALUE, second = Integer.MIN_VALUE;
for(int val : arr) {
if(val > first) {
second = first;
first = val;
} else if(val > second && val != first) {
second = val;
}
}
System.out.println("Second Largest: " + second);
}
}

---

2. Check if Array is Sorted

import java.util.Scanner;

public class IsSorted {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) arr[i] = sc.nextInt();
boolean sorted = true;
for(int i = 1; i < n; i++) {
if(arr[i] < arr[i - 1]) {
sorted = false;
break;
}
}
System.out.println("Is Sorted: " + sorted);
}
}
---

3. Remove Duplicates (Sorted Array)

import java.util.Scanner;

public class RemoveDuplicates {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) arr[i] = sc.nextInt();
int j = 1;
for(int i = 1; i < n; i++) {
if(arr[i] != arr[i - 1]) arr[j++] = arr[i];
}
for(int i = 0; i < j; i++) System.out.print(arr[i] + " ");
}
}

---

4. Left Rotate by One

import java.util.Scanner;

public class LeftRotate {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) arr[i] = sc.nextInt();
int temp = arr[0];
for(int i = 1; i < n; i++) arr[i - 1] = arr[i];
arr[n - 1] = temp;
for(int val : arr) System.out.print(val + " ");
}
}

---

5. Leaders in Array

import java.util.Scanner;

public class LeadersInArray {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) arr[i] = sc.nextInt();
int max = Integer.MIN_VALUE;
for(int i = n - 1; i >= 0; i--) {
if(arr[i] > max) {
System.out.print(arr[i] + " ");
max = arr[i];
}
}
}
}

---

6. Count Frequency of Elements

import java.util.*;

public class FrequencyCount {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
HashMap<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < n; i++) {
int num = sc.nextInt();
map.put(num, map.getOrDefault(num, 0) + 1);
}
System.out.println(map);
}
}

---

7. Maximum Subarray Sum (Kadane)

import java.util.Scanner;

public class MaxSubarray {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) arr[i] = sc.nextInt();
int max = arr[0], current = arr[0];
for(int i = 1; i < n; i++) {
current = Math.max(arr[i], current + arr[i]);
max = Math.max(max, current);
}
System.out.println("Max Subarray Sum: " + max);
}
}

---

8. Move Zeros to End

import java.util.Scanner;

public class MoveZeros {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) arr[i] = sc.nextInt();
int j = 0;
for(int i = 0; i < n; i++) {
if(arr[i] != 0) {
int temp = arr[i];
arr[i] = arr[j];
arr[j++] = temp;
}
}
for(int val : arr) System.out.print(val + " ");
}
}

---

9. Find Missing Number

import java.util.Scanner;

public class MissingNumber {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // n numbers from 1 to n (one missing)
int[] arr = new int[n - 1];
int sum = n * (n + 1) / 2;
for(int i = 0; i < n - 1; i++) {
arr[i] = sc.nextInt();
sum -= arr[i];
}
System.out.println("Missing Number: " + sum);
}
}

---

10. Find Duplicates

import java.util.*;

public class FindDuplicates {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
HashSet<Integer> seen = new HashSet<>();
HashSet<Integer> dupes = new HashSet<>();
for(int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
if(!seen.add(arr[i])) dupes.add(arr[i]);
}
System.out.println("Duplicates: " + dupes);
}
}

You might also like