Java Program
Java Program
Java Program
import java.util.*;
public class Merge{
public static void main(String args[]){
int[] arr1={1,3,6,9,5};
int[] arr2={4,7,2,9,0};
Set<Integer> mergedSet=new TreeSet<>();
for(int num:arr1){
mergedSet.add(num);
}
for(int num:arr2){
mergedSet.add(num);
}
int[] mergedarray= new int[mergedSet.size()];
int index=0;
for(int num:mergedSet){
mergedarray[index++]=num;
}
System.out.println(Arrays.toString(mergedarray));
}
}
OUTPUT:
[0, 1, 2, 3, 4, 5, 6, 7, 9]
2.SLIDING WINDOW
import java.util.*;
int k = 3;
deque.poll();
deque.pollLast();
deque.offer(i);
if (i >= k - 1) {
OUTPUT:
7776534
import java.util.*;
class HelloWorld {
public static void main(String[] args) {
int[] array1={1,1,3,2,2,2,5,8,9,8};
Arrays.sort(array1);
int count=1;
for(int i=0;i<array1.length-1;i++)
{
if(array1[i]==array1[i+1])
count++;
else
{
System.out.println(array1[i]+": "+count);
count=1;
}
}
}
}
OUTPUT:
1: 2
2: 3
3: 1
5: 1
8: 2
import java.util.*;
class AlternativeString{
public static void main (String[] args){
int arr[] = {5,2,8,7,4,3,9};
int n = arr.length;
Arrays.sort(arr);
int i = 0, j = n-1;
while (i < j) {
System.out.print(arr[j--] + " ");
System.out.print(arr[i++] + " ");
}
if (n % 2 != 0)
System.out.print(arr[i])
}
}
OUTPUT:
9283745
OUTPUT:
Sum:15
elements:1 3 2 9
8.COMBINATION SUM
import java.util.ArrayList;
import java.util.List;
public class CombinationSum {
public static void main(String[] args) {
int[] arr = {8, 3, 4, 7, 9};
int target = 7;
List<List<Integer>> results = new ArrayList<>();
findCombinations(arr, target, 0, new ArrayList<>(), results);
// Print the results
for (List<Integer> result : results) {
System.out.println(result);
}
}
private static void findCombinations(int[] arr, int target, int start, List<Integer> combination,
List<List<Integer>> results) {
if (target == 0) {
results.add(new ArrayList<>(combination));
return;
}
if (target < 0) {
return;
}
for (int i = start; i < arr.length; i++) {
combination.add(arr[i]);
findCombinations(arr, target - arr[i], i + 1, combination, results);
combination.remove(combination.size() - 1);
}
}
}
OUTPUT:
[3, 4]
[7]
import java.util.*;
import java.util.Scanner;
public class New{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of array: ");
int a = scanner.nextInt();
int[] arr1=new int[a];
System.out.println("Enter the elements: "); for(int i=0;i<a;i++){
arr1[i]=scanner.nextInt();
}
Set<Integer> array = new TreeSet<>();
for(int num:arr1){
array.add(num);
}
for(int num:array){
System.out.print(num+", ");
}
}
}
OUTPUT:
12.JACK SUNDAYS(ERROR)
OUTPUT:
import java.util.Scanner;
public class New{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int a=scanner.nextInt();
int[] one=new int[a];
System.out.print("Enter the elements: ");
int peak = 0;
int bottom =0;
for(int i=0; i<a;i++){
one[i]=scanner.nextInt();
}
for(int i=1;i<a-1;i++){
if(one[i]>one[i+1] && one[i]>one[i-1]){
peak++;
}
if(one[i]<one[i+1] && one[i]<one[i-1]){
bottom++;
}
}
System.out.println("Peak: "+peak);
System.out.print("Bottom: "+bottom);
}
}
OUTPUT:
1567000000