Join Regular Classroom : Visit ClassroomTech

Tech Mahindra Coding Question Solve | Set-B | Part II | codewindow.in

Duplicates
Write a function to remove duplicate elements from an array and return the array with unique integer values only.

Input Specification:
input1: An integer array containing numbers
input2: Length of the above integer array

Output Specification:
Return an integer array after removal of duplicate values

Example 1

Input:

input1: 11 11 11 13 13 20
input2: 6

Output:
11 13 20

Solution: In Java

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

class CodeWindow
{
    static void removeduplicates(int a[], int n) {
        if (n == 0 || n == 1) {
            for(int i=0; i<n; i++)
                System.out.println(a[i]);
            return;
        }
        Arrays.sort(a);
        int j = 0;
        for (int i = 0; i < n - 1; i++)
            if (a[i] != a[i + 1])
                a[j++] = a[i];
  
        a[j++] = a[n - 1];
        for (int i = 0; i < j; i++)
            System.out.print(a[i] + " ");
    }
    
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] str = br.readLine().split(" ");
		int arr[] = new int[str.length];
		for(int i=0; i<str.length; i++)
		    arr[i] = Integer.parseInt(str[i]);
		int n = Integer.parseInt(br.readLine());
		removeduplicates(arr, n);
	}
}

Perfect Sums
A perfect sum is the sum of two or more elements of n array which is equal to given number. Your task is to write a function to compute the number of
perfect sums existing in an array.
Note: If no such sums exists, return 999. Also, if the input array is not an integer array, then also return 999.
Input Specification:
input1: An integer value representing the length of the integer array.
input2: An integer array of length N.
input3: An integer value representing the number, the perfect sum must be equal to.

Output Specification:

Return the number of perfect sums existing in the array.

Solution: In Java

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

class CodeWindow
{
    public static int sumSubsets(int set[], int n, int target) {

        int x[] = new int[set.length];
        int j = set.length - 1;
 
        while (n > 0) {
            x[j] = n % 2;
            n = n / 2;
            j--;
        }
 
        int sum = 0;
        for (int i = 0; i < set.length; i++)
            if (x[i] == 1)
                sum = sum + set[i];
                
        if (sum == target) return 1;
        else return 0;
    }
    
    public static void findSubsets(int[] arr, int sum){
        int x = (int)Math.pow(2, arr.length);
        int count = 0;
        for (int i = 1; i < x; i++)
            if (sumSubsets(arr, i, sum) == 1)
                count++;
        if(count==0) System.out.println(999);
        else System.out.println(count);
    }
    
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		String[] str = br.readLine().split(" ");
		int arr[] = new int[n];
		for(int i=0; i<n; i++)
		    arr[i] = Integer.parseInt(str[i]);
		int sum = Integer.parseInt(br.readLine());
		findSubsets(arr, sum);
	}
}

Regions on a Plane
Mr. Professor is great scientist, but he is not able to find a solution to one problem. There are N straight lines that are not parallel, and no three lines go through the same point. The lines divide the plane into M regions. Write a function to find out the maximum number of such regions he can get on the plane.
Input Specification:
Input1: An integer N representing the number of straight lines(0<=N<=100)
Output Specification:
Return the maximum number of regions.

Solution: In Java

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


class CodeWindow {
	public static void main (String[] args) throws Exception
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		int regions = n * (n + 1) / 2 + 1;
		System.out.println(regions);
	}
}

Also Checkout

Recent Posts
Categories
Pages