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]);
}
}
}
}
}