The Programming Project

Saturday, March 16, 2019

ISC COMPUTER SCIENCE PRACTICAL 2002 SOLVED QUESTION 2 : ARRAY



  Write a program to do the following:- Read an integer n. (where can be at most 50.) then
  read n integers one by one and store them in array data from,
  index 0 to n-1. Now we are to rearrange the integers in data in the following ways:-
  Find the maximum value in data and put it in the center of the array (that is at n/2),
  find the next largest value and put it to its right, and so on alternating right and
  left until all integers in data are done. For example if the array is initially:
  Sample:
 Give the number of integers: 6
 INPUT: 5 1 9 8 2 4
 OUTPUT:  2 5 9 8 4 1
 The idea behind this program is as follows
1. Input the array length and array elements.
2. Sort the array in descending order using any standard sorting technique
3. Find the middle postion according as array length is even or odd
4. Transfer the first element of the sorted ( descending order) in a new array
5. From the 2nd elemets onward, alternately put the elements on the right and left 
    of middle position and  increase the right and left counter
To achieve Step 3, the following code has put to work
pos = (array_length%2==0)? (array_length/2-1):(array_length/2);

To uderstand it consider the following input:
Give the number of integers: 6

 INPUT: 5 1 9 8 2 4
Since 6 is even, pos = 6/2-1=2
After sorting the array it becomes
9 8 5 4 2 1
Now the first element '9' goes at position 0 of  new array.
Now these elements 8[1], 5[2],4[3],2[4],1[5] of need to be inserted old array 
where a[b] represent value at position b.
New array at this point looks like:

Empty[0],Empty[1],9[2],Empty[3],Empty[4],Empty[5]

Now 8[1] will go at Empty[3]
        5[2] will got at Empty[1]
        4[3] will go at Empty[4]
        2[4] will go at Empty[0]
        1[5] will go at Empty[5]
See that elements at odd position goes at position pos+right 
       1 -----  2+1
       3 -----  2+2
       5 -----  2+3
So, it seems obvious to declare a right counter to 1 and increment it by 1 for the next step.
Similar arguments, goes for elements at even position.

The following code carries out this process.
       int pos,right=1,left=1;
       pos = (array_length%2==0)? (array_length/2-1):(array_length/2);
       final_arr[pos]=arr[0];
       for(int t=1;t<=array_length-1;t++) {
              if(t%2 != 0) {
                     final_arr[pos+right]=arr[t];
                     right++;
                     }
              else {
                     final_arr[pos-left]=arr[t];
                     left++;
                     }
              }

See that the for loop runs from 1 to 5
whenever the postion is odd, we are putting the element at the right of middle position
and increasing the right counter and similarly for the elements of even position.


JAVA CODE

import java.util.*;
public class ISC2002Q2 { 
       public static void main(String[] args) {
              int N;
              Scanner in = new Scanner(System.in);
              do {
                 System.out.println("Enter the value of N (<=50):");
                 N = in.nextInt();
                 if(N >50)
                        System.out.println("INVALID INPUT,TRY AGAIN");
                 }while(N>50);
              Array obj = new Array(N);
              System.out.println("Enter the elements of the array:");
              obj.inputArray();
              System.out.println("Entered elements of the array:");
              obj.display();
              obj.generateNewArray();
              in.close();                 
       }
}
class Array {
       public void generateNewArray() {
              int temp;
              // sort the array in descending order
              for(int i = 0; i < array_length; i++) {
            for(int j=1; j < (array_length-i); j++) {
               if(arr[j-1] < arr[j]) {
                  temp = arr[j-1];
                  arr[j-1] = arr[j];
                  arr[j] = temp;
               }
            }
         }
       // putting the elements at required position   
       int pos,right=1,left=1;
       pos = (array_length%2==0)? (array_length/2-1):(array_length/2);
       final_arr[pos]=arr[0];
       for(int t=1;t<=array_length-1;t++) {
              if(t%2 != 0) {
                     final_arr[pos+right]=arr[t];
                     right++;
                     }
              else {
                     final_arr[pos-left]=arr[t];
                     left++;
                     }
              }
       // displaying the new array
       System.out.println();
       System.out.println("Array after required insertion:");
       for(int i=0;i<array_length;i++)
              System.out.print(final_arr[i]+" ");
       }
       public void display() {
              for(int i=0;i<array_length;i++)
                     System.out.print(arr[i]+" ");
       }
       public void inputArray() {
              Scanner in = new Scanner(System.in);
              for(int i=0;i<array_length;i++)
                     arr[i]=in.nextInt();
              in.close();
       }
       Array(int N) {
              array_length = N;
              arr = new int[array_length];
              final_arr = new int[array_length];
              }
       private int array_length;
       private int[] arr;
       private int[] final_arr;
}

ISC COMPUTER SCIENCE PRACTICAL 2002 SOLVED QUESTION 1 : TWIN PRIME


ISC COMPUTER SCIENCE PRACTICAL SOLVED

  Twin Primes are consecutive prime numbers whose difference is 2.

  For example, (3, 5), (11, 13), (17, 19) are all twin primes.
  We define the distance of any twin prime pair from n as 
  :- minimum (abs(n-p1), abs(n-p2))
  where (p1,p2) is a pair of twin prime.

  where abs( ) returns the absolute value of its argument and minimum( ) returns the        smaller of its two arguments.   

  Write a program that reads in a positive integer n and prints out the twin prime that    has least distance from n.

  For example if n is 30 the pair is (29, 31), if n is 13 pair is (11, 13), if n is 49      the pair is (41, 43).   
  If n is 54 the pair is (59, 61).

The logic is as follows:
1. Input the number N
2. Find the Twin prime just after or from N 
(it might happen the (N,N+2) is a twin prime)
3. Find the Twin prime just before N
4. Compared the distances of these Twin Primes from N using 
the formula given.
5. Print the Twin Prime having least distance from N.

The distance method is 
private int distanceOfTwinPrime(int N,int p) {

             return (Math.min(Math.abs(N-p),Math.abs(N-(p+2))));
            
       }


The method private int generateTwinPrime(int N,String s)
finds the "after" or "before" Twin Primes of N by sending
(N,"after") or (N,"before") as required:
The logic is if (N,N+2) is not a pair of twin prime we
increment N to N+1 and check for (N+1,N+1+2)
flag = false;

             while(flag==false) {
             if(isPrime(N)==true && isPrime(N+2)==true)   
                    flag=true;
             else
                    N++;
This particualr block finds the next Twin prime.
Similarly for (N,"before")
private boolean isPrime(int n) returns true in n is prime or else
it returns false.

private int distance_after;

private int distance_before;
stores the distance of N from the "after" Twin Prime
stores the distance of N from the "before" Twin Prime

For N = 1 and 2 The closest Twin prime is (3,5). This can be
printed straight forward.

JAVA CODE

import java.util.*;
public class ISC2002Q1 { 
    public static void main(String[] args) {
       int N;
       Scanner in = new Scanner(System.in);
       System.out.println("Enter the value of N:");
       N = in.nextInt();
       TwinPrime obj = new TwinPrime(N);
       obj.displayNearestTwinPrime();
       in.close();
        }  
    }
class TwinPrime {
       public void displayNearestTwinPrime() {
             flag = false;
             distance_after = distanceOfTwinPrime(number,generateTwinPrime(number,"after"));
             distance_before = distanceOfTwinPrime(number,generateTwinPrime(number,"before"));
             flag = distance_after < distance_before ? true : false;
             if(flag)
                    System.out.print("Nearest Twin Prime for "+number+" is"+"("+generateTwinPrime(number,"after")+","+(generateTwinPrime(number,"after")+2)+").");
             else
                    System.out.print("Nearest Twin Prime for "+number+" is"+"("+generateTwinPrime(number,"before")+","+(generateTwinPrime(number,"before")+2)+").");
            
       }
       private int distanceOfTwinPrime(int N,int p) {
             return (Math.min(Math.abs(N-p),Math.abs(N-(p+2))));
            
       }
       private int generateTwinPrime(int N,String s) { // if (p,p+2) is the twin prime it returns p
             if(s=="after") {
             flag = false;
             while(flag==false) {
             if(isPrime(N)==true && isPrime(N+2)==true)   
                    flag=true;
             else
                    N++;
             } // finds the pair of twin prime after N
            if(N<=2)
             return 3;
             else
                 return (N);
             }
             else {
                    flag = false;
                    while(flag==false) {
                    if(isPrime(N)==true && isPrime(N+2)==true)   
                          flag=true;
                    else
                          N--;
                    }
              if(N<=2)
              return 3;
             else
                 return (N);  // finds the pair of twin prime before N
             }
       }
       private boolean isPrime(int n) {
              flag = true;
              if (n==1)
                    return false;
         else {
                for(int i = 2; i<=n/2;i++)
                          if(n%i==0) {
                             flag=false;
                             break;
                          }
                return(flag);
              }
         }
       TwinPrime(int N){
             number = N;
       }
       private int number;
       private boolean flag;
       private int distance_after;
       private int distance_before;
}