0% found this document useful (0 votes)
13 views25 pages

Coding-Skills-Arrays - Solutions

coding skill

Uploaded by

rizvihasan931
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views25 pages

Coding-Skills-Arrays - Solutions

coding skill

Uploaded by

rizvihasan931
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

1.

Write Java program that checks if two arrays contain the same elements
Ans:

public class Test {

public static void main( String [] args) {

int [] a = {10, 8, 15, 13, 10};


//
int [] b = {10, 13, 10, 15, 8};

if( a.length!=b.length)
{
System.out.println("Not equal arrays");

System.exit(0);
}

boolean flag1 = false;

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


{
flag1 = false;

for(int j=0; j<b.length;j++)


{
if(a[i]==b[j])
{
flag1=true;
break;
}
}

if(!flag1)
{

break;
}

boolean flag2 = false;

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


{
flag2 = false;

for(int j=0; j<b.length;j++)


{
if(b[i]==a[j])
{
flag2=true;
break;
}
}

if(!flag2)
{

break;
}

if(flag1 && flag2)


{
System.out.println("Same");
}
else
{
System.out.println("not Same");
}

}
}

2. Write Java program that checks if two arrays contain the same no of elements, same
values and same order of elements.
Solution :

public class Test {

public static void main( String [] args) {

int [] a = {10, 8, 12, 18, 15};


//
int [] b = {10, 8, 12, 18, 15};

boolean flag=true;
for(int i=0; i<a.length;i++)
{
if(a[i]!=b[i])
{
flag = false;
break;
}
}

if(flag)
{
System.out.println("Same");
}
else
{
System.out.println("Not Same");
}

3. Write program to swap two arrays of equal length.


Solution :

public class Test {

public static void main( String [] args) {

int [] a = {20, 18, 2, 8, 5};


//
int [] b = {10, 8, 12, 18, 15};

if( a.length!=b.length)
{
System.out.println("Not equal length, can
not swap ");

System.exit(0);
}

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


{
int temp = a[i];

a[i] = b[i];

b[i] = temp;
}

System.out.println("After Swapping ");

System.out.println("Elements of array a ");

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


{
System.out.println(a[i]);
}

System.out.println("Elements of array b ");

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


{
System.out.println(b[i]);
}

4. Program to display reverse of array


Ans :

public class Test {

public static void main( String [] args) {

int [] a = {20, 18, 2, 8, 5};

for(int i=a.length-1; i>=0; i--)


{
System.out.println(a[i]);
}

}
5 write a program to make reverse of an array .
Solution:

public class Test {

public static void main( String [] args) {

int [] a = {20, 18, 2, 8, 5, 10};

System.out.println("Initial elements ");

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


{
System.out.print(a[i]+" ");
}

for(int i=0, j=a.length-1; i<j ; i++, j--)


{

int temp = a[i];

a[i] = a[j];

a[j] = temp;
}

System.out.println("\n after elements ");

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


{
System.out.print(a[i]+" ");
}

6. write program to merge two arrays

public class Test {


public static void main( String [] args) {

int [] a = {20, 18, 2, 8, 5, 10};

int [] b = {20, 18, 2, 12, 8, 5, 6, 10};

int [] c = new int[a.length+b.length];

int i = 0;

for(i = 0; i<a.length; i++)


{
c[i] = a[i];
}

// i = 6

for(int j =0; j<b.length; j++, i++)


{
c[i] = b[j];

for(i=0; i<c.length; i++)


{
System.out.print(c[i]+" ");
}

7. Write program to find second largest element in array of elements


Ans:
Solution1 :

public class Test {

public static void main(String[] args) {

int [] a = {15, 5, 13, 18, 31, 35, 29};

int max1 = a[0];

int max2 = a[0];


for(int i = 1; i<a.length; i++)
{
if(a[i]>max1)
{
max2 = max1;

max1=a[i];
}

if(a[i]>max2 || a[i]<max1)
{
max2 = a[i];
}
}

System.out.println("max1 = "+max1);

System.out.println("max2 = "+max2);

Solution2 :

Using sorting

public class Test {

public static void main(String[] args) {

int [] a = {15, 5, 13, 18, 31, 35, 29};

// sorting in ascending order, bubble sorting

for(int i =0; i<a.length-1; i++)


{
for(int j = 0; j<a.length-1-i; j++)
{
if(a[j]>a[j+1])
{
int temp = a[j];

a[j] = a[j+1];

a[j+1] = temp;
}
}
}
int large2 = a[a.length-1];

for(int i = a.length-1; i>=1; i--)


{
if(a[i]!=a[i-1])
{
large2 = a[i-1];

break;
}
}

if(a[a.length-1]!=large2)
{
System.out.println("Second Largest number =
"+large2);
}
else
{
System.out.println("Dont have second largest");
}

8. Write program to find second least element in array of elements


Ans :

Solution1 :
package com.tech.mnrao.abc;

public class Test {

public static void main(String[] args) {

int [] a = {10, 12, 3, 15, 29, 8};

int min1 = a[0];

int min2 = a[0];


for(int i = 1; i<a.length; i++)
{
if(a[i]<min1)
{
min2 = min1;

min1=a[i];
}

if(a[i]>min1 || a[i]<min2)
{
min2 = a[i];
}
}

System.out.println("min1 = "+min1);

System.out.println("min2 = "+min2);

Another solution using Sorting :

public class Test {

public static void main(String[] args) {

int [] a = {15, 15, 17, 15, 15, 15, 15};

// sorting in descending order, bubble sorting

for(int i =0; i<a.length-1; i++)


{
for(int j = 0; j<a.length-1-i; j++)
{
if(a[j]<a[j+1])
{
int temp = a[j];

a[j] = a[j+1];

a[j+1] = temp;
}
}
}

int least2 = a[a.length-1];

for(int i = a.length-1; i>=1; i--)


{
if(a[i]!=a[i-1])
{
least2 = a[i-1];

break;
}
}

if(a[a.length-1]!=least2)
{
System.out.println("Second Least number =
"+least2);
}
else
{
System.out.println("Dont have second leasts");
}

9. write program to remove duplicate elements in array


Ans:

public class Test {

public static void main(String[] args) {

int [] a = {15, 5, 17, 12, 18, 12, 15};

int [] b = new int[a.length];

int index = 0;

b[index++]=a[0];

for(int i = 1; i<a.length; i++)


{
boolean flag = false;

for(int j=0;j<index; j++ )


{
if(a[i]==b[j])
{
flag = true;
break;
}

if( !flag)
{
b[index++]=a[i];
}

int [] c = new int[index];

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


{
c[i] = b [i];
}

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


{
System.out.println(c[i]);
}

10. How to find largest number less than a given number.


Ans :

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

int [] a = {15, 5, 13, 18, 31, 35, 29};


Scanner sc = new Scanner( System.in);

System.out.println("Enter a Number ");

int num = sc.nextInt();

for(int i =0; i<a.length-1; i++)


{
for(int j = 0; j<a.length-1-i; j++)
{
if(a[j]>a[j+1])
{
int temp = a[j];

a[j] = a[j+1];

a[j+1] = temp;
}
}
}

boolean flag = true;

for(int i = a.length-1; i>=0; i--)


{
if(a[i]<num)
{
System.out.println("Largest number less
than your number : "+a[i]);
flag = false;
break;
}
}

if(flag)
{
System.out.println( "don't have number less
than your number");
}

11. how to fine unionAll of two arrays


Union all means merge.
Ans :
import java.util.Scanner;

public class Test {

public static void main(String[] args) {

int [] a = {15, 5, 13, 18, 31, 35, 29};

int [] b = {5, 13, 17, 21, 35, 22, 28, 12};

int [] c = new int[a.length+b.length];

int index = 0;

for(int n : a)
{
c[index++]=n;
}

for(int n : b)
{
c[index++]=n;
}

for(int i =0; i<c.length-1; i++)


{
for(int j = 0; j<c.length-1-i; j++)
{
if(c[j]>c[j+1])
{
int temp = c[j];

c[j] = c[j+1];

c[j+1] = temp;
}
}
}

for(int n : c)
{
System.out.print(n+" ");
}

}
}

12. how to find union() of array elements


union means it combines both arrays and removes duplicate

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

int [] a = {15, 5, 13, 18, 31, 35, 29};

int [] b = {5, 13, 17, 21, 35, 22, 28, 12};

int [] c = new int[a.length+b.length];

int index = 0;

for(int n : a)
{
c[index++]=n;
}

for(int n : b)
{
c[index++]=n;
}

int [] d = new int[c.length];

index = 0;

d[index++]=c[0];

for(int i = 1; i<c.length; i++)


{
boolean flag = false;

for(int j=0;j<index; j++ )


{
if(c[i]==d[j])
{
flag = true;
break;
}

if( !flag)
{
d[index++]=c[i];
}

int [] e = new int[index];

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


{
e[i] = d[i];
}

System.out.println("Unique elements are ");

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


{
System.out.print(e[i]+" ");
}

13. How to find intersection of two arrays in Java?


Intersection means common elements

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

int [] a = {15, 5, 13, 18, 31, 35, 29, 44, 22, 55};

int [] b = {5, 13, 15, 21, 35, 22, 15, 12};

int clength = a.length;

if(a.length<b.length)
{
clength = a.length;
}
else
{
clength = b.length;
}

int [] c = new int[clength];

int index = 0;

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


{
int count = 0;

for(int j=0; j<b.length; j++)


{
if(a[i]==b[j])
{
count++;
if(count==1)
{
c[index++]=a[i];
}
}

if(count>1)
{
break;
}
}

int d [] = new int[index];

for(int i = 0; i<d.length;i++)
{
d[i] = c[i];
}

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


{
System.out.print(d[i]+" ");
}
}

}
14. write program to remove given element from array.

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

int [] a = {15, 5, 13, 44, 31, 35, 29, 44, 22, 55};

Scanner sc = new Scanner(System.in);

System.out.println("enter the element to remove ");

int num = sc.nextInt();

int count = 0;

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


{
if(num==a[i])
{
count++;
}
}

int [] b = new int[a.length-count];

int index = 0;

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


{
if( num!=a[i] )
{
b[index++]=a[i];
}
}

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


{
System.out.print(b[i]+" ");
}
}

}
15.Java program to insert an element at end of an Array.

Ans:

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

int [] a = {15, 5, 13, 44, 31, 35, 29, 44, 22, 55};

int [] b = new int[a.length+1];

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number to insert at


the end ");

int num = sc.nextInt();

int i ;

for(i=0; i<a.length;i++)
{
b[i] = a[i];
}

b[i] = num;

System.out.println("before adding ");

for(i = 0; i<a.length; i++)


{
System.out.print(a[i]+" ");
}

a = b;

System.out.println("\n after adding ");

for(i = 0; i<a.length; i++)


{
System.out.print(a[i]+" ");
}

}
}

16.Java program to insert an element at begining of an Array.

Ans :

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

int [] a = {7, 55, 13, 18, 31, 35, 29};

Scanner sc = new Scanner( System.in);

System.out.println("Enter the number to insert at


begining ");

int num = sc.nextInt();

int [] b = new int[a.length+1];

b[0] = num;

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


{
b[i+1] = a[i];
}

System.out.println("Before insert elements ");

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


{
System.out.print(a[i]+" ");
}

a = b;

System.out.println("\n after insert elements ");

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


{
System.out.print(a[i]+" ");
}

17.Java Program to delete element at end of Array.

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

int [] a = {7, 55, 13, 18, 31, 35, 29};

int [] b = new int[a.length-1];

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


{
b[i] = a[i];
}

System.out.println("Before deleting elements ");

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


{
System.out.print(a[i]+" ");
}

a = b;

System.out.println("\n after deleting elements ");

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


{
System.out.print(a[i]+" ");
}

18.Java Program to delete element at beginning of Array.


import java.util.Scanner;

public class Test {

public static void main(String[] args) {

int [] a = {7, 55, 13, 18, 31, 35, 29};

int [] b = new int[a.length-1];

for(int i =1; i<a.length;i++)


{
b[i-1] = a[i];
}

System.out.println("Before delete elements ");

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


{
System.out.print(a[i]+" ");
}

a = b;

System.out.println("\n after delete elements ");

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


{
System.out.print(a[i]+" ");
}

19.Write a program in Java to find largest and smallest number in array.

public class Test {


public static void main(String[] args) {

int [] a = {10, 18,15,25,14,3,12};

int max = a[0];

int min = a[0];

for( int i=1; i<a.length; i++)


{

if(a[i]>max)
{
max=a[i];
}

if(a[i]<min)
{
min=a[i];
}

System.out.println("max = "+max);

System.out.println("min = "+min);
}
}
20.Write a program in Java to find top two maximum number in
array?

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

int [] a = {7, 55, 13, 18, 31, 35, 29};

// sorting in ascending order, bubble sorting

for(int i =0; i<a.length-1; i++)


{
for(int j = 0; j<a.length-1-i; j++)
{
if(a[j]>a[j+1])
{
int temp = a[j];

a[j] = a[j+1];

a[j+1] = temp;
}
}
}

int large2 = a[a.length-1];

for(int i = a.length-1; i>=1; i--)


{
if(a[i]!=a[i-1])
{
large2 = a[i-1];

break;
}
}
if(a[a.length-1]!=large2)
{
System.out.println("First largest number =
"+a[a.length-1]);

System.out.println("Second Largest number =


"+large2);
}
else
{
System.out.println("Dont have second largest,
only one largest = "+a[a.length-1]);
}

21. Java Program to delete element from array at given index.

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

int [] a = {7, 55, 13, 18, 31, 35, 29};

Scanner sc = new Scanner(System.in);

System.out.println("Enter index value, to remove


element");

int index = sc.nextInt();

if(index>a.length-1)
{
System.out.println("given index is not found in
array");
System.exit(0);
}

int [] b = new int[a.length-1];


int bindex = 0;

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


{
if( i != index)
{
b[bindex++]=a[i];
}
}

System.out.println("Before removing ");

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


{
System.out.print(a[i]+" ");
}

a = b;

System.out.println("\n after removing ");

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


{
System.out.print(a[i]+" ");
}
}

Java Coding Questions on Array

1. Write a program in Java for, Given two arrays 1,2,3,4,5 and


2,3,1,0,5 find which number is not present in the second array.

14. Java program to insert


element at a given location in Array.

15. Java program to find all


pairs in array of integers whose sum is equal to given number.

You might also like