Join Regular Classroom : Visit ClassroomTech

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

Regions on a Plane

Mr. Professor is a 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

Example 1

Input:
2

Output:
4

Explanation:
Given the above scenario, 2 lines divide the plane into 4 regions. Therefore, 4 is returned as the output.

Example 2

Input:
3

Output:
7

Explanation:
Given the above scenario 3 lines divide the plane into 7 regions. Therefore 7 returned as output.

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);
	}
}

String within String 

Given two strings ‘X’ and ‘Y’ (length(X)>=1, length(Y)<=10000), find out if ‘Y’ is contained in ‘X’. Return “yes”, if ‘Y’ is contained in ‘X’, “no”, if not.

Input Specification:
input1: the string ‘X’
input2: the string ‘Y’

Output Specification:
Return “yes” if string “Y” is contained in string “X”, else return “no”.

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));
		String x = br.readLine();
		String y = br.readLine();
		if(x.indexOf(y) != -1)
		    System.out.println("yes");
		else
		    System.out.println("no");
	}
}

Also Checkout

Recent Posts
Categories
Pages