Pages

Wednesday 7 June 2017

For the given position find the perfect square else make it to next nearest perfect square,then make that to first element and align remaining in ascending order

public class PerfectSquare {

public static void main(String[] args) {
int a[] = {3,12,61,22,78,7};
int sq=0,sqt=0,pos,fp,tmp;
Scanner s = new Scanner(System.in);
System.out.println("Enter the position :");
pos = s.nextInt();
        
/*Making perfect square*/
while(sq != a[pos]){  
        sqt =(int) Math.sqrt(a[pos]);
        sq  = sqt * sqt;
       
        if(sq!=a[pos])
          a[pos]++;
        }

/*Swapping perfect square element to first position*/
fp=a[0];
a[0]=a[pos];
a[pos]=fp;

/*Sorting elements in ascending order - Bubble sort*/
for(int j=0;j<a.length;j++){
for(int k=0;k<a.length;k++){
if(j==0 || k==0)
continue;
if(a[k]>a[j]){
tmp  = a[k];
a[k] = a[j];
a[j] = tmp;
}
}
}

for(int i:a){
System.out.print(i+" ");
}
s.close();
}
}

Output:

Enter the position :
3

25 3 7 12 61 78 

Friday 2 June 2017

Order the elements of array in consecutive maximum and minimum sequence

public class FirstMaxFirstMin {
void rearrange(int arr[], int n)
{
   // Auxiliary array to hold modified array
   int temp[] = new int[n];
 
   // Indexes of smallest and largest elements
   // from remaining array.
   int small=0, large=n-1;
 
   // To indicate whether we need to copy remaining
   // largest or remaining smallest at next position
   boolean flag = true;
 
   // Store result in temp[]
   for (int i=0; i<n; i++)
   {
       if (flag)
           temp[i] = arr[large--];
       else
           temp[i] = arr[small++];
 
       flag = !flag;
   }
 
   // Copy temp[] to arr[]
   for (int i=0; i<n; i++)
       arr[i] = temp[i];
}

public static void main(String[] args) {
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int n = arr.length;
        
FirstMaxFirstMin f=new FirstMaxFirstMin();
 
System.out.println("Original Array");
for (int i = 0; i < n; i++)
System.out.print(arr[i]+" ");

f.rearrange(arr, n);

System.out.println("\nModified Array");
for (int i = 0; i < n; i++)
System.out.print(arr[i]+" ");
}

}

Output:

Original Array
1 2 3 4 5 6 7 8 9 
Modified Array
9 1 8 2 7 3 6 4 5 

Merge Two arrays without duplicates and sort the array to ascending order

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class MergeArrays {

public static void main(String[] args) {
int a[]={1,2,3,4};
int b[]={10,9,8,7,5,4,0,-6,0,0,0};
int count = 0,temp;

Set<Integer> nodup = new HashSet<Integer>();

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

int f[] = new int[nodup.size()];
int z = 0;

Iterator<Integer> it = nodup.iterator();
while(it.hasNext()){
f[z]=it.next();
z++;
}

for(int l=0;l<f.length;l++){
for(int m=0;m<f.length;m++){
if(f[l]<f[m]){
temp=f[l];
f[l]=f[m];
f[m]=temp;
}
}
}

for(int k=0;k<f.length;k++){
System.out.print(f[k]+" ");
}

}
}

Output:

-6 0 1 2 3 4 5 7 8 9 10 

Pascal's Triangle using Java

import java.util.Scanner;

public class PascalsTriangle {

public static void main(String[] args) {
int rows,coef=1,space,i,j;
Scanner s = new Scanner(System.in);
System.out.println("Enter the number of rows : ");
rows = s.nextInt();
for(i=0;i<rows;i++)
{
for(space=1;space<=rows-i;space++)
{
System.out.print("  ");
}

for(j=0;j<=i;j++)
{
if(i==0 || j==0){
coef=1;
}else{
coef=coef*(i-j+1)/j;
}

System.out.printf("%4d",coef);
}
System.out.println();
}
        s.close();
}
}

Output:

Enter the number of rows : 
5
             1
           1   1
         1   2   1
       1   3   3   1
     1   4   6   4   1


Code Review

 SOLID Principles S – Single Responsibility Principle There should never be more than one reason for a class to change. O – Open-Closed Prin...