Arrays programms

 Arrays coding round interview programms.



1] BinarySearch


package com.hefshine.arrayscw;

import java.util.Scanner;

public class BinarySearch {
int size, search;
int mid;

void binary() {

Scanner sc = new Scanner(System.in);

System.out.println("enter size of an array: ");
size = sc.nextInt();

System.out.println("enter element of an array ");
int[] array = new int[size];
for (int i = 0; i < size ; i++) {
array[i] = sc.nextInt();
}
System.out.println("enter search :");
search = sc.nextInt();

int first = 0;
int last = size - 1;
boolean flag = true;
while (first <= last) {

mid = (first + last) / 2;

if (array[mid] == search) {
flag = false;
System.out.println("element fount at : " + mid);
break;
} else if (array[mid] < search) {
first = mid + 1;
} else {
last = mid - 1;
}

}
if (flag) {

System.out.println("invalid element");
}
}
}

package com.hefshine.arrayscw;

public class BinarySearchMain {

public static void main(String[] args) {
// TODO Auto-generated method stub
BinarySearch obj=new BinarySearch();
obj.binary();
}

}





2] Number of unique pairs in an array. Give nan array of N elements, that ask  is to find all the unique pairs that can be formed using the elements of given array.(March Monthly).


package com.hefshine.arrayscw;

import java.util.Scanner;

import com.hefshine.method.MainAdditionDemo;

public class Ex6UniPair {

void pair() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter size");
int size = sc.nextInt();
int[] a = new int[size];
System.out.println("enter element");
for (int i = 0; i < size; i++) {
a[i] = sc.nextInt();
}
System.out.println("after removing duplicate element :");
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
if (a[i] == a[j]) {
a[i] = 0; // Remove duplicate first

}
}
if(a[i]!=0)
{
System.out.println(a[i]);
}
}

int count = 0;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if ((a[i] != 0) && a[j] != 0) {

count++;
System.out.println("{ " + a[i] + "," + a[j] + "}");
}

}

}
System.out.println("no of pairs is :" + count);
}

public static void main(String[] args) {
Ex6UniPair obj = new Ex6UniPair();
obj.pair();
}

}




3] stack implementation of an array


package com.hefshine.arrayscw;

import java.util.Scanner;

public class Ex7Stack {

int top,size;
int []a;
Scanner sc=new Scanner(System.in);
void input() {
System.out.println("Enter size");
size=sc.nextInt();
a=new int[size];
System.out.println("Enter element");
}
void push()
{
top=0;
while(top!=size) 
{
a[top++]=sc.nextInt();
}
}
void pop()
{
top=top-1;
while(top!= -1) 
{
System.out.println(a[top--]);
}
}
public static void main(String[] args) 
{
// TODO Auto-generated method stub
Ex7Stack obj=new Ex7Stack();
obj.input();
obj.push();
obj.pop();

}


}





4]. Write a Java program to calculate the average value of array elements 


package com.hefshine.arrayscw;

import java.util.Scanner;

public class Example1 {

int size;

void input() {

Scanner sc = new Scanner(System.in);

System.out.println("enter size of an array");
size = sc.nextInt();
int[] num = new int[size];
System.out.println("enter element of an array");
for (int i = 0; i < size; i++) {
num[i] = sc.nextInt();
}

float sum = 0;
float avg = 1;
for (int i = 0; i < size; i++) {
sum = sum + num[i];
}
avg = sum / size;
System.out.println("average value of an array element is " + avg);
}

public static void main(String args[]) {
Example1 obj = new Example1();
obj.input();
}
}





5] place/shift negative number at last in an array


package com.hefshine.arrayscw;

import java.util.Scanner;

public class PositiveNegative {

void input() {

int size;

Scanner sc = new Scanner(System.in);
System.out.println("enter size of an array1");
size = sc.nextInt();
int[] array = new int[size];

System.out.println("enter element of an array1");
for (int i = 0; i < size; i++) {

array[i] = sc.nextInt();

}

for (int i = 0; i < array.length; i++) {
for (int j = i + 1; j < array.length; j++) {

if (array[j] > 0) {
int t;
t = array[i];
array[i] = array[j];
array[j] = t;

}
}
}

for (int k = 0; k < array.length; k++) {
System.out.println(array[k] + "\t");
}
}

public static void main(String[] args) {
PositiveNegative obj = new PositiveNegative();
obj.input();
}
}




6] Insertion sort



package com.hefshine.arrayscw;

import java.util.Scanner;

public class SelectionSort {

int size;

void selection() {

Scanner sc = new Scanner(System.in);

// SelectionSort obj = new SelectionSort();

System.out.println("enter size of an array");
int size = sc.nextInt();
int[] num = new int[size];
System.out.println("enter element of an array");
for (int i = 0; i < size; i++) {
num[i] = sc.nextInt();
}

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

for (int j = i + 1; j < l; j++) {
if (num[i] > num[j]) 
{

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

}

}
// for (int x : num) {
// System.out.println(x);
//
// }
display(num); // caling method
}

void display(int num[]) {
for (int x : num) {
System.out.println(x);
// System.out.println();

}
}

public static void main(String[] args) {

SelectionSort obj = new SelectionSort();
obj.selection();

}
}




7]Transpose matrix


package com.hefshine.arrayscw;

import java.util.Scanner;

public class Transpose {

void input() {
int size;

Scanner sc = new Scanner(System.in);
System.out.println("enter size of an array1");
size = sc.nextInt();
int[][] array = new int[size][size];

System.out.println("enter element of an array1");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
array[i][j] = sc.nextInt();

}
}
System.out.println("display matrix");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
System.out.print(array[i][j] + "\t"); // or  array[j][i]

}
System.out.println();
}

int[][] array2 = new int[size][size];

for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
array2[i][j] = array[j][i];

}
}
System.out.println("display transpot matrix");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {

System.out.print(array2[i][j] + "\t");

}
System.out.println();
}

}

public static void main(String[] args) {
Transpose obj = new Transpose();
obj.input();

}
}



display matrix
1 2 3
4 3 2
3 4 5
display transpot matrix
1 4 3
2 3 4
3 2 5

\


 Section 2


1] Sort array using Array.sort();



package com.hefshine.arrayhw;

import java.util.Arrays;
import java.util.Scanner;

public class Array1Sort {

void input() {
Scanner sc = new Scanner(System.in);
System.out.println("enter size of an array");
int size = sc.nextInt();
int[] array = new int[size];
System.out.println("enter element into array");
{
for (int i = 0; i < size; i++) {

array[i] = sc.nextInt();
}
}
String[] array2 = new String[size];
System.out.println("enter element into array");
{
for (int i = 0; i < size; i++) {

array2[i] = sc.nextLine();

}
}
for (int i = 0; i < size; i++) {
Arrays.sort(array);
Arrays.sort(array2);
}

System.out.println("sorted arrays" + Arrays.toString(array));
System.out.println("sorted arrays" + Arrays.toString(array2));
}

public static void main(String[] args) {
Array1Sort obj = new Array1Sort();
obj.input();
}
}



2] 2D array 



package com.hefshine.arrayhw;

import java.util.Scanner;

public class Array2Darray {

int[][] input() {

int size;
Scanner sc = new Scanner(System.in);
System.out.println("enter size of an array1");
size = sc.nextInt();
int[][] array = new int[size][size];

System.out.print("enter element of an array1");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
array[i][j] = sc.nextInt();

}
}
// display();
return array;

}

void display() {

int[][] array = input();
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
System.out.print(" " + array[i][j]);

}
System.out.println();
}

}

public static void main(String[] args) {
Array2Darray obj = new Array2Darray();
obj.display();

}
}




3]  finding index on an array



package com.hefshine.arrayhw;

import java.util.Scanner;

public class Array3FindIndex {

void input() {
int size;

Scanner sc = new Scanner(System.in);
System.out.println("enter size of an array");
size = sc.nextInt();
int[] num = new int[size];
System.out.println("enter element of an array");
for (int i = 0; i < size; i++) {

num[i] = sc.nextInt();

}

display(num, size);

}

void display(int[] num, int size) {
boolean flag = true;
for (int i = 0; i < size; i++) {
flag = false;
System.out.println(+num[i] + " element found at index:" + i);

}
if (flag) {
System.out.println("not found");
}
}

public static void main(String[] args) {
// TODO Auto-generated method stub

Array3FindIndex obj = new Array3FindIndex();
obj.input();
}

}



4] Remove specific Element from array



package com.hefshine.arrayhw;

import java.util.Scanner;

public class Array4RemoveSpecificE {

void input() {
int size, i, del, count = 0;

Scanner sc = new Scanner(System.in);
System.out.println("enter size of an array");
size = sc.nextInt();
int[] num = new int[size];

System.out.println("enter element of an array");
for (i = 0; i < size; i++) {

num[i] = sc.nextInt();

}
System.out.print("Enter Element to be Delete : ");
del = sc.nextInt();

for (i = 0; i < num.length-1; i++) {
if (num[i] == del) {

num[i] = num[i + 1];     // it simply shift 

}
}

System.out.print("\nNow the New Array is :\n");
for (i = 0; i < num.length-1 ; i++) {
System.out.print(num[i] + "   ");

}
}

public static void main(String[] args) {

Array4RemoveSpecificE obj = new Array4RemoveSpecificE();
obj.input();

}
}





5]Copy Array



package com.hefshine.arrayhw;

import java.util.Scanner;

public class Array5CopyArray {

void input() {
int size, i;

Scanner sc = new Scanner(System.in);
System.out.println("enter size of an array");
size = sc.nextInt();
int[] num = new int[size];
int[] num2 = new int[num.length];

System.out.println("enter element of an array");
for (i = 0; i < size; i++) {

num[i] = sc.nextInt();

}

for (i = 0; i < num.length; i++) {
num2[i] = num[i];
}
//display
for (i = 0; i < num2.length; i++)

System.out.print(num2[i] + " ");

}

public static void main(String[] args) {
// TODO Auto-generated method stub
Array5CopyArray obj = new Array5CopyArray();
obj.input();
}
}






Comments

Popular posts from this blog

OBJECT ORIENTED CONCEPTS