Join Regular Classroom : Visit ClassroomTech

New Coding Program | Set-6 SOLVED | codewindow.in

Program 1. Geological Sorting

A team of geologists attempting to measure the difference in carbon-dated volcanic materials and non-volcanic materials of the same carbon-dated age in order to establish a base for testing.
You are given 2 arrays, volcanic and nonVolcanic. Write a program to find a list of materials with identical dates between both arrays. Return this list in descending order (i.e, the highest age followed by the second-highest, with the lowest age at the end).

For example, you are given the following two arrays:
volcanic = [7000, 13400, 7000, 14000], and
nonVolcanic = [7000, 13400, 150000, 7000]
Your return array should show: result = [13400, 7000, 7000]
The date 7000 is present twice in both input arrays. Therefore, there are two matches and both should be returned in the output array. However, there is no matching number for 150000 in volcanic or 14000 in nonVolcanic, so these two numbers should not be returned as result.

Function Description:

The program must print an array (in descending order) of the dates of matching pairs that can be created between volcanic and nonVolcanic.

Constraints:
5<=n, 0<=1, 000
6600<=volcanic[i], nonVolcanic[j]<=18300

Input Format for Custom Testing
The first line contains an integer, n, indicating the size of the array volcanic.
Each of the next n lines contains an integer, volcanic[i].
The next line contains an integer, 0, indicating the size of the array nonVolcanic.
Each of the next 0 lines contains an integer, nonVolcanic[j].

Example 1

Input:
5
7000
7000
12000
13000
6900
7
6910
7010
7000
12000
18000
15000
10450

Solution: In Java

import java.util.*;
import java.lang.*;
import java.io.*;

class CodeWindow
{
	public static void main (String[] args) throws Exception
	{
		// your code goes here
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int m = Integer.parseInt(br.readLine());
		int[] volcanic = new int[m];
		for(int i=0; i<m; i++)
		    volcanic[i] = Integer.parseInt(br.readLine());
		int n = Integer.parseInt(br.readLine());
		int[] nonVolcanic = new int[n];
		boolean[] nonVolcanicMatch = new boolean[n];
		for(int i=0; i<m; i++) {
		    nonVolcanic[i] = Integer.parseInt(br.readLine());
		    nonVolcanicMatch[i] = false;
		}
		int newArr[] = new int[n];
		int k = 0;
		for(int i=0; i<m; i++) {
		    for(int j=0; j<n; j++) {
		        if(nonVolcanicMatch[j] == false && volcanic[i] == nonVolcanic[j]) {
		            newArr[k++] = volcanic[i];
		            nonVolcanicMatch[j] = true;
		            break;
		        }
		    }
		}
		Arrays.sort(newArr);
		int i=0;
		while(i < k) {
		    System.out.print(newArr[n-i-1] + " ");
		    i++;
		}
	}
}

Program 2. Array Operation: Ascending Order

You are given an array A of length N.
In one operation you can remove a number either from the start of the array, from the end of the array, or from the middle of the array.

Write a program to find the number of operations to be performed, so the remaining elements will be in ascending order.

Input Format
The first line of input contains one integer N.
The second line of input contains N integers of array A.

Constraints
1<=N<=1000
1<=Ai<=100000

Output Format
The output contains the number of operations to be performed. So the remaining elements will be in non-decreasing order.

Example 1

Input:
6
2 3 4 5 6 1

Output:

1

Solution: In Java

import java.util.*;
import java.lang.*;
import java.io.*;

class CodeWindow
{
    static int lis(int arr[], int n) {
        int result = 0;
        int[] lis = new int[n];
        for (int i = 0; i < n; i++ )
            lis[i] = 1;

        for(int i = 1; i < n; i++)
            for(int j = 0; j < i; j++)
                if(arr[i] > arr[j] && lis[i] < lis[j] + 1)
                    lis[i] = lis[j] + 1;
     
        for (int i = 0; i < n; i++ )
            if (result < lis[i])
                result = lis[i];
     
        return result;
    }
    
    static int minimumNumberOfDeletions(int arr[], int n) {
        int len = lis(arr, n);
        return (n - len);
    }
    
	public static void main (String[] args) throws Exception
	{
		// your code goes here
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int m = Integer.parseInt(br.readLine());
		String[] str = br.readLine().split(" ");
		int[] arr = new int[m];
		for(int i=0; i<m; i++)
		    arr[i] = Integer.parseInt(str[i]);
		System.out.println(minimumNumberOfDeletions(arr, m));
	}
}
JECA Questions

Also Checkout

Recent Posts
Categories
Pages