Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Java Program

Java program 3

Uploaded by

nagpalmokesh18
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Java Program

Java program 3

Uploaded by

nagpalmokesh18
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

REGISTRATION NUMBER: MCA/10071/24

ROLL NUMBER: 71
NAME: MOKESH NAGPAL
DATE: 27 AUGUST 2024

Q1> Write a program to find duplicate values in an array of


integers
Q2> Write a Java program to find duplicate value in two arrays
Q3> Write a java program to find sum of two elements of a given
array equal to given integer
Q4> Write a java program arrange the elements of an array of
integers so that all
negative integers appear before all positive integers.
Q5> Write a java program rearrange a given array of unique
elements such that every second element of the array is greater
than its left and right elements.
Q6> WAP to find the transpose of a matrix without using the
dedicated library function.
Q1> Write a program to find duplicate values in an array of
integers
/*
Write a program to find duplicate values in an array of integers
*/
import java.util.Scanner;

public class Main {

private void input(int[] arr, int n, Scanner sc) {


System.out.println("Input " + n + " values continuously: \n");
for (int i = 0; i < n; i++) {
System.out.println("Input " + (i + 1) + "th value in the array:
");
arr[i] = sc.nextInt();
}
}

private void m_sort(int[] arr, int lower, int upper) {


if (upper <= lower)
return;
int mid = (lower + upper) / 2;
m_sort(arr, lower, mid);
m_sort(arr, mid + 1, upper);
merge(arr, lower, mid, upper);
}

private void merge(int[] arr, int lower, int mid, int upper) {
int n1 = mid - lower + 1;
int n2 = upper - mid;

int[] left = new int[n1];


int[] right = new int[n2];

for (int i = 0; i < n1; i++)


left[i] = arr[lower + i];
for (int i = 0; i < n2; i++)
right[i] = arr[mid + 1 + i];

int i = 0, j = 0, k = lower;

while (i < n1 && j < n2) {


if (left[i] <= right[j]) {
arr[k++] = left[i++];
} else {
arr[k++] = right[j++];
}
}

while (i < n1) {


arr[k++] = left[i++];
}

while (j < n2) {


arr[k++] = right[j++];
}
}
private void findDuplicates(int[] arr, int n) {
System.out.println("Duplicate values in the array are:");
boolean found = false;

for (int i = 0; i < n - 1; i++) {


if (arr[i] == arr[i + 1]) {
found = true;
System.out.println(arr[i]);
while (i < n - 1 && arr[i] == arr[i + 1]) {
i++;
}
}
}

if (!found) {
System.out.println("No duplicates found.");
}
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.println("This Java program finds the duplicate values in
an array of integers.");

System.out.println("Input the length of the array: ");


int n = sc.nextInt();
int[] arr = new int[n];

Main obj = new Main();


obj.input(arr, n, sc);

obj.m_sort(arr, 0, n - 1);

obj.findDuplicates(arr, n);
}
}

OUTPUT
Q2> Write a Java program to find duplicate value in two arrays
/*
Write a Java program to find duplicate value in two arrays
*/
import java.util.Scanner;

public class Main {

private void input(int[][] arr, int rows, int cols, Scanner sc) {
System.out.println("Input the values for the array: ");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("Input value for [" + i + "][" + j + "]:
");
arr[i][j] = sc.nextInt();
}
}
}

private void find_common_elements(int[][] arr1, int rows1, int cols1,


int[][] arr2, int rows2, int cols2) {
System.out.println("Common elements between the two 2D arrays
are:");
boolean found = false;

for (int i = 0; i < rows1; i++) {


for (int j = 0; j < cols1; j++) {
int element = arr1[i][j];

for (int x = 0; x < rows2; x++) {


for (int y = 0; y < cols2; y++) {
if (element == arr2[x][y]) {
found = true;
System.out.println(element);
break;
}
}
}
}
}

if (!found) {
System.out.println("No common elements found.");
}
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.println("This program finds the common elements between


two 2D arrays.");

System.out.print("Input number of rows for the first array: ");


int rows1 = sc.nextInt();
System.out.print("Input number of columns for the first array: ");
int cols1 = sc.nextInt();

int[][] arr1 = new int[rows1][cols1];


Main obj = new Main();
obj.input(arr1, rows1, cols1, sc);

System.out.print("Input number of rows for the second array: ");


int rows2 = sc.nextInt();
System.out.print("Input number of columns for the second array: ");
int cols2 = sc.nextInt();

int[][] arr2 = new int[rows2][cols2];


obj.input(arr2, rows2, cols2, sc);

obj.find_common_elements(arr1, rows1, cols1, arr2, rows2, cols2);


}
}

OUTPUT
Q3> Write a java program to find sum of two elements of a given
array equal to given integer
/*
Write a java program to find sum of two elements of a given array equal to
given integer
*/
import java.util.Scanner;

public class Main {

private void input(int[] arr, int length, Scanner sc) {


System.out.println("Input the values for the array: ");
for (int i = 0; i < length; i++) {
System.out.print("Input value for [" + i + "]: ");
arr[i] = sc.nextInt();
}
}

private void find_all_pairs_sum(int[] arr, int length, int number) {


int left = 0, right = length - 1;
boolean found = false;

while (left < right) {


int sum = arr[left] + arr[right];

if (sum == number) {
System.out.println("Pair found: " + arr[left] + ", " +
arr[right]);
found = true;
left++;
right--;
} else if (sum < number) {
left++;
} else {
right--;
}
}

if (!found) {
System.out.println("No pairs found.");
}
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.print("Input number of elements in the array: ");


int length = sc.nextInt();

if (length < 2) {
System.out.println("Invalid input. Array must have at least two
elements.");
return;
}

int[] arr = new int[length];


Main obj = new Main();
obj.input(arr, length, sc);

// Sorting the array


obj.m_sort(arr, 0, length - 1);

System.out.print("Input the target sum: ");


int number = sc.nextInt();

obj.find_all_pairs_sum(arr, length, number);


}

private void m_sort(int[] arr, int lower, int upper) {


if (upper <= lower) return;
int mid = (upper + lower) / 2;
m_sort(arr, lower, mid);
m_sort(arr, mid + 1, upper);
merge(arr, lower, mid, upper);
}

private void merge(int[] arr, int lower, int mid, int upper) {
int n1 = mid + 1 - lower;
int n2 = upper - mid;
int[] arr1 = new int[n1];
int[] arr2 = new int[n2];

for (int i = 0; i < n1; i++) arr1[i] = arr[lower + i];


for (int i = 0; i < n2; i++) arr2[i] = arr[mid + 1 + i];

int i = 0, j = 0, k = lower;

while (i < n1 && j < n2) {


if (arr1[i] <= arr2[j]) {
arr[k++] = arr1[i++];
} else {
arr[k++] = arr2[j++];
}
}

while (i < n1) arr[k++] = arr1[i++];


while (j < n2) arr[k++] = arr2[j++];
}
}

OUTPUT

Q4> Write a java program arrange the elements of an array of


integers so that all negative integers appear before all positive
integers.
/*
Write a java program arrange the elements of an array of integers so that
all
negative integers appear before all positive integers.
*/
import java.util.Scanner;

public class Main {

private void input(int[] arr, int length, Scanner sc) {


System.out.println("Input the values for the array: ");
for (int i = 0; i < length; i++) {
System.out.print("Input value for [" + i + "]: ");
arr[i] = sc.nextInt();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("This program finds the common elements between


two 2D arrays.");

System.out.print("Input number of length for the array: ");


int length = sc.nextInt();
int[] arr = new int[length];
Main obj = new Main();
obj.input(arr, length, sc);
System.out.println("\nOutput is as follows: ");
for(int i = 0;i <length; i++){
if(arr[i]<0)
System.out.print(arr[i]+"\t");
}
for(int i = 0;i <length; i++){
if(arr[i]>=0)
System.out.print(arr[i]+"\t");
}
}
}

OUTPUT
Q5> Write a java program rearrange a given array of unique
elements such that every second element of the array is greater
than its left and right elements.
/*
Write a java program rearrange a given array of unique elements such that
every second
element of the array is greater than its left and right elements.
*/
import java.util.Scanner;

public class Main {

private void input(int[] arr, int length, Scanner sc) {


System.out.println("Input the values for the array: ");
for (int i = 0; i < length; i++) {
System.out.print("Input value for [" + i + "]: ");
arr[i] = sc.nextInt();
}
}

private void rearrangeArray(int[] arr, int length) {


for (int i = 1; i < length; i += 2) {
if (arr[i - 1] > arr[i]) {
swap(arr, i - 1, i);
}
if (i + 1 < length && arr[i + 1] > arr[i]) {
swap(arr, i + 1, i);
}
}
}

private void swap(int[] arr, int i, int j) {


int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.print("Input number of elements for the array: ");


int length = sc.nextInt();

if (length <= 1) {
System.out.println("Array needs at least two elements for
rearrangement.");
return;
}

int[] arr = new int[length];


Main obj = new Main();
obj.input(arr, length, sc);

obj.rearrangeArray(arr, length);

System.out.println("\nRearranged array:");
for (int value : arr) {
System.out.print(value + "\t");
}
}
}

OUTPUT
Q6> WAP to find the transpose of a matrix without using the
dedicated library function.
/*
WAP to find the transpose of a matrix without using the dedicated library
function.
*/
import java.util.Scanner;

public class Main {

private void inputMatrix(int[][] matrix, int rows, int cols, Scanner


sc) {
System.out.println("Input the matrix elements: ");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("Element [" + i + "][" + j + "]: ");
matrix[i][j] = sc.nextInt();
}
}
}

private int[][] transposeMatrix(int[][] matrix, int rows, int cols) {


int[][] transpose = new int[cols][rows];

for (int i = 0; i < rows; i++) {


for (int j = 0; j < cols; j++) {
transpose[j][i] = matrix[i][j];
}
}
return transpose;
}

private void displayMatrix(int[][] matrix, int rows, int cols) {


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.print("Input the number of rows in the matrix: ");


int rows = sc.nextInt();

System.out.print("Input the number of columns in the matrix: ");


int cols = sc.nextInt();

int[][] matrix = new int[rows][cols];


Main obj = new Main();

obj.inputMatrix(matrix, rows, cols, sc);

System.out.println("\nOriginal Matrix:");
obj.displayMatrix(matrix, rows, cols);

int[][] transposedMatrix = obj.transposeMatrix(matrix, rows, cols);

System.out.println("\nTransposed Matrix:");
obj.displayMatrix(transposedMatrix, cols, rows);
}
}

OUTPUT

You might also like