Java Program

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

PROGRAMS

1.MERGE SORTED UNIQUE ARRAY

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.*;

public class SlidingWindowMaximum {

public static void main(String[] args) {

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

int k = 3;

// Deque to store indexes of useful elements in every window

Deque<Integer> deque = new LinkedList<>();

// Traverse the array

for (int i = 0; i < arr.length; i++) {

// Remove elements out of the current window

if (!deque.isEmpty() && deque.peek() < i - k + 1) {

deque.poll();

// Remove elements that are smaller than the current element

while (!deque.isEmpty() && arr[deque.peekLast()] < arr[i]) {

deque.pollLast();

// Add the current element at the back of the deque

deque.offer(i);

// The largest element in the window is at the front of the deque

if (i >= k - 1) {

System.out.print(arr[deque.peek()] + " ");

OUTPUT:
7776534

3.ELEMENT FREQUENCY COUNTER

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

4.THRESHOLD VALUE ,FIND COUNT


public class ThresholdPaths {
public static void main(String[] args) {
int[] arr = {5, 8, 10, 13, 6, 2};
int threshold = 3;
int count = 0;
for (int num : arr) {
while (num > 0) {
int step = Math.min(threshold, num);
num -= step;
count++;
}
}
System.out.println("count = " + count);
}
}
OUTPUT:
Count =17

5.ALTERNATE HIGH LOW ARRAY

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

6.PRINT THE ELEMENTS OF THE ARRAY GREATER THAN ITS PREVIOUS

public class GreaterThanPrevious {


public static void main(String[] args) {
int[] arr = {2, -3, -4, 5, 9, 7, 8};
// Print the first element
System.out.print(arr[0] + " ");
// Initialize a variable to keep track of the maximum value seen so far
int maxSoFar = arr[0];
// Traverse the array starting from the second element
for (int i = 1; i < arr.length; i++) {
if (arr[i] > maxSoFar) {
System.out.print(arr[i] + " ");
maxSoFar = arr[i];
// Update the maximum value seen so far
}
}
}
}
OUTPUT:
259

7.LARGEST SUM CONTIGOUS SUBARRAY

public class MaxSumNonNegativeSubarray {


public static void main(String[] args) {
int[] arr = {2, 7, -5, 1, 3, 2, 9, -7};
int maxSum = 0;
int currentSum = 0;
int start = 0;
int end = 0;
int tempStart = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] >= 0) {
if (currentSum == 0) {
tempStart = i;
//subarray
}
currentSum += arr[i];
if (currentSum > maxSum) {
maxSum = currentSum;
start = tempStart;
end = i;
}
} else {
currentSum = 0; // Reset current sum if a negative number is found
}
}
// Print the results
System.out.println("Sum: " + maxSum);
System.out.print("Elements: ");
for (int i = start; i <= end; i++) {
System.out.print(arr[i] + " ");
}
}
}

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]

9.UNIQUE SORTED ARRAY INPUT

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:

Enter the size of array: 10


Enter the elements:
2345567892
2, 3, 4, 5, 6, 7, 8, 9,
10.ERROR
11.AIRPORT SECURITY OFFICIALS……

public class RiskSorter {


public static void main(String[] args) {
int[] arr = {1, 0, 2, 0, 1, 0, 2}; // Example input array
sortArray(arr);
// Print the sorted array
for (int num : arr) {
System.out.print(num);
}
}
public static void sortArray(int[] arr) {
int low = 0, mid = 0, high = arr.length - 1;
while (mid <= high) {
switch (arr[mid]) {
case 0:
// Swap arr[low] and arr[mid], then increment both
swap(arr, low++, mid++);
break;
case 1:
// Move to the next element
mid++;
break;
case 2:
// Swap arr[mid] and arr[high], then decrement high
swap(arr, mid, high--);
break;
}
}
}
public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
OUTPUT:
0001122

12.JACK SUNDAYS(ERROR)

13.DECIMAL NUMBER INTO BINARY NUMBER

public class DecimalToBinary {


public static void main(String[] args) {
int decimalNumber = 123; // Example input
String binaryString = convertToBinary(decimalNumber);
System.out.println(binaryString); // Output the binary representation
}
public static String convertToBinary(int decimalNumber) {
// Convert the decimal number to a binary string
String binaryString = Integer.toBinaryString(decimalNumber);
return binaryString;
}
}
OUTPUT:
1111011

14.XOR OF ADJACENT ELEMENTS(OUTPUT IS NOT IN PROPER FORMAT)

public class XorAdjacent {


public static void main(String[] args) {
int[] arr = {1, 0, 1, 0, 1, 1}; // Input array
int iterations = 5; // Number of iterations

// Perform XOR operation for the specified number of iterations


for (int i = 0; i < iterations; i++) {
arr = xorAdjacent(arr);
printArray(arr);
}
}

// Method to perform XOR on adjacent elements


private static int[] xorAdjacent(int[] arr) {
int[] newArr = new int[arr.length - 1];
for (int i = 0; i < arr.length - 1; i++) {
newArr[i] = arr[i] ^ arr[i + 1]; // XOR operation
}
return newArr;
}

// Method to print the array


private static void printArray(int[] arr) {
for (int num : arr) {
System.out.print(num);
}
System.out.println(); // New line after printing the array
}
}
OUTPUT:
11110
0001
001
01
1

15.DIGIT PRODUCT PRIZE CALCULATOR

public class PriceCalculator {


public static void main(String[] args) {
int N = 5244;
int product = 1;
int a=N;
while (N > 0) {
int digit = N % 10; // Get the last digit
product *= digit; // Multiply the digit to the product
N /= 10; // Remove thdigit
}
System.out.println("The price of the item with code " + a + " is: " + product);
}
}

OUTPUT:

The price of the item with code 5244 is: 160

16. PEAK AND BOTTOM PROGRAM

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:

Enter the size of the array: 3


Enter the elements: 2 3 4
Peak: 0
Bottom: 0

17. ZEROS SHOULD COME END

public class New{


public static void main(String args[]){
int[] one={0,1,0,5,0,0,6,0,7,0};
int count=0; for(int i=0;i<one.length;i++){
if(one[i]!=0){
System.out.print(one[i]);
}
else{
count++;
}
}
for(int i=0;i<count;i++){
System.out.print(0);
}
}
}
OUTPUT:

1567000000

You might also like