Functions
F2P1 - BEST PROGRAMMER
Best Programmer
Baldwin High School's Best Programmer Contest is organized today and the contest hones the students coding skills by making them solve different challenges. Based on the speed and accuracy with which the students finish the challenges, the Event coordinators will rank the participants and reward them.
The entry level challenge was just one problem which the students has to program for. The problem reads like:
A positive integer, n, is said to be perfect if the sum of its proper divisors equals the number itself. (Proper divisors include 1 but not the number itself.) If this sum is less than n, the number is deficient, and if the sum is greater than n, the number is abundant.
The Event coordinators wanted to prepare the answer key for all the problems given in the contest so as to evaluate the submissions of the participants.
Hence create a class named NumberType with the following method.
| Method Name |
Description
|
| int findType(int) | This method return 1 if the given integer is a deficient number, return 0 if it is a perfect number and return -1 if it is a abundant number. |
Create a driver class called Main. In the Main method, obtain input from the user in the console and display the number along with its classification by calling the findType method present in the NumberType class.
[Note: Strictly adhere to the Object Oriented Specifications given in the problem statement.
All class names, attribute names and method names should be the same as specified in the problem statement. Create separate classes in separate files.]]
Input Format:
The input consists of an integer that corresponds to the given number.
Output Format:
Output should display if the given number is a perfect, abundant or deficient number.
Refer sample input and output for formatting specifications.
Sample Input 1:
4
Sample Output 1:
4 is a deficient number
Sample Input 2:
6
Sample Output 2:
6 is a perfect number
Sample Input 3:
12
Sample Output 3:
12 is an abundant number
Solution:
import java.util.*;
public class Main{
public static void main(String args[]){
//get inputs
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
int sum = NumberType.findType (x);
if(sum<0)
{
System.out.println(x+" is a deficient number");
}
else if(sum==0)
{
System.out.println(x+" is a perfect number");
}
else if(sum>0)
{
System.out.println(x+" is an abundant number");
}
//type your code here
}
}
public class NumberType{
public static int findType (int x)
{
//type your code here
int r=0;
int sum=0;
for(int i=1;i<=x/2;i++)
{
if(x%i==0)
{
sum+=i;
}
}
if(sum<x)
r=-1;
else if(sum==x)
r=0;
else if(sum>x)
r=1;
return r;
}
}
F2P2 - NICE NUMBER
Nice Number
" The Greatest Furniture Expo" is a biggest fair exhibiting furniture products, services and equipment, interior services, decoration plans, modular kitchen accessories, bedroom furniture, stylish sittings etc in the Furniture industry. It is a 4-day event and on the inaugural day of the event, the Event coordinators have announced for a Lucky lottery contest.
According to the Lucky lottery, the visitors’ entry tickets are collected and the visitors whose ticket numbers are nice numbers are chosen as winners and attractive discount coupons are distributed to the winners. A nice number is a positive integer which has exactly 4 divisors.
The Event coordinators wanted to know if a specific’s entry ticket number is a nice number or not.
Hence create a class named NiceNumber with the following method.
| Method Name | Description |
| int findType(int) | This method return 1 if the number is nice or return 0 if it is not a nice number. |
Create a driver class called Main. In the Main method, obtain input from the user in the console and display "Nice" if the given ticket number is a nice number. Print "Not Nice" otherwise by calling the findType method present in NiceNumber class.
[Note: Strictly adhere to the Object Oriented Specifications given in the problem statement.
All class names, attribute names and method names should be the same as specified in the problem statement. Create separate classes in separate files.]
Input Format:
First line of the input is an integer that corresponds to the entry ticket number of a visitor.
Output format:
Output should display "Nice"(without quotes) if the given ticket number is a nice number. Print "Not Nice"(without quotes) otherwise.
Refer sample input and output for formatting specifications.
Sample Input 1:
6
Sample Output 1:
Nice
Sample Input 2:
4
Sample Output 2:
Not Nice
import java.util.*;
public class Main{
public static void main(String args[]){
//get inputs
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
int result = NiceNumber.findType (x);
if(result==1)
System.out.println("Nice");
else
System.out.println("Not Nice");
//type your code here
}
}
public class NiceNumber{
public static int findType (int x)
{
//type your code here
int r=0;
int count=0;
for(int i=1;i<=x;i++)
{
if(x%i==0)
{
count++;
}
}
if(count==4)
r=1;
else
r=0;
return r;
}
}
F2P3 - WINTER CHALLENGE
Winter Challenge
Swapna is a regular reader of Youth Digest magazine. The magazine has a whole host of fun and interesting facts from around for the youth especially that encourage interactivity and enhances their imagination.
"Winter Challenge" is an event announced in the December month edition of the magazine. Readers of the magazine who are between 10 to 15 years can participate in the special challenge. Those readers who participate and give the correct answer for the challenge will avail exciting gift coupons. According to the event, the challenge published was:
Given 0 < x < m, where x and m are integers, the modular inverse of x is the unique integer n, 0 < n < m, such that the remainder upon dividing x × n by m is 1.
For example,
if x = 4 and m = 17, we get n = 13
4 x 13 = 52 = 17 x 3 + 1, so the remainder when 52 is divided by 17 is 1, and thus 13 is the inverse of 4 modulo 17.
Swapna wants your help to find the correct answer for the problem based on the inputs given in the magazine.
Hence create a class named ModInverse with the following method.
"Winter Challenge" is an event announced in the December month edition of the magazine. Readers of the magazine who are between 10 to 15 years can participate in the special challenge. Those readers who participate and give the correct answer for the challenge will avail exciting gift coupons. According to the event, the challenge published was:
Given 0 < x < m, where x and m are integers, the modular inverse of x is the unique integer n, 0 < n < m, such that the remainder upon dividing x × n by m is 1.
For example,
if x = 4 and m = 17, we get n = 13
4 x 13 = 52 = 17 x 3 + 1, so the remainder when 52 is divided by 17 is 1, and thus 13 is the inverse of 4 modulo 17.
Swapna wants your help to find the correct answer for the problem based on the inputs given in the magazine.
Hence create a class named ModInverse with the following method.
Method Name
|
Description
|
int findValue(int,int)
|
This method returns the modular inverse n or return -1 if there is no such integer n.
|
Create a driver class called Main. In the Main method, obtain input from the user in the console and display the modular inverse n, or -1 if there is no such integer n by calling the findValue method present in the ModInverse class.
[Note: Strictly adhere to the Object Oriented Specifications given in the problem statement.
All class names, attribute names and method names should be the same as specified in the problem statement. Create separate classes in separate files.]
Input Format:
First line of the input is an integer that corresponds to x.
Second line of the input is an integer that corresponds to m.
Output format:
Output should display the appropriate value or print -1 otherwise.
Refer sample input and output for formatting specifications.
Sample Input 1:
4
17
Sample Output 1:
13
Sample Input 2:
6
10
Sample Output 2:
-1
import java.util.*;
public class Main{
public static void main(String args[]){
//get inputs
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
int m=sc.nextInt();
int result = ModInverse.findValue(x,m);
//type your code here
System.out.println(result);
}
}
public class ModInverse{
public static int findValue (int x, int m)
{
//type your code here
int i;
x=x%m;
for(i=1;i<m;i++)
{
if((x*i)%m==1)
{
return i;
}
}
return -1;
}
}
F2P6 - HIGH SCHOOL TOPCODER
High School TopCoder
Green Wood High School is set to premier a programming tournament for high school-aged math and science students across the country. Based on this contest, the school has called in for all the interested candidates to take up a qualifying test at the school premises.Before the qualifier, the Event coordinators chose the problemsets and wanted to code it beforehand to ease the evaluation procedure. They wanted your help to code few of the problemsets, one of which was involving Fibonacci series. We all know the Fibonacci sequence, each term of it is the sum of the two previous terms. In this problem, we need to find just the last digit of a Fibonacci series termed as F(n), where n is got as input.
Here, create a class named Fibonacci with the following method.
Method Name
|
Description
|
int fiboLastDigit(int)
|
This method should return the last digit of the term F (n).
|
Create a driver class called Main. In the Main method, obtain input from the user in the console and display the the last digit of the term F (n) by calling the fiboLastDigit method present in the Fibonacci class.
[Note: Strictly adhere to the Object Oriented Specifications given in the problem statement.
All class names, attribute names and method names should be the same as specified in the problem statement. Create separate classes in separate files.]
Input Format:
The input consists of an integer n(0 <= n <= 10000).
Output Format:
Output the last digit of the term F (n).
Refer sample input and output for formatting specifications.
Sample Input 1:
5
Sample Output 1:
5
Sample Input 2:
9
Sample Output 2:
4
import java.util.*;
public class Main{
public static void main(String args[]){
//get inputs
Scanner sc=new Scanner(System.in);
long n=sc.nextLong();
long result = Fibonacci.fiboLastDigit(n);
System.out.println(result);
//tpe your code here
}
}
public class Fibonacci{
public static long fiboLastDigit(long n)
{
//type your code here
long a,b,i,fib=0,rem;
a=1;
b=1;
i=2;
if(n>2)
{
while(i!=n)
{
fib=a+b;
a=b;
b=fib;
i++;
}
rem=fib%10;
return rem;
}
else if(n==0)
{
return 0;
}
else
{
return 1;
}
}
}
S5P10-BOB'S CHALLENGE
Bob's Challenge
Stella and friends have set out on a vacation to Manali. They have booked accommodation in a resort and the resort authorities headed by Bob, organize Camp fires every night as a part of their daily activities. Stella volunteered herself for an activity called the "Stick Game".
Stella was given a total of N sticks. Length of i-th stick is Ai. Bob insists Stella to choose any four sticks and to make a rectangle with those sticks as its sides. Bob warns Stella not to break any of the sticks, she has to use sticks as a whole.
Also, Bob wants that the rectangle formed should have the maximum possible area among all the rectangles that Stella can make. Stella takes this challenge up and overcomes it. You have to help her know whether it is even possible to create a rectangle. If yes, then tell the maximum possible area of rectangle.
Input Format:
The first line of the input contains a single integer N denoting the number of sticks.
The second line of each test case contains N space-separated integers A1, A2, ...,AN denoting the lengths of sticks.
Output Format:
Output a single line containing an integer representing the maximum possible area for rectangle or output -1, if it's impossible to form any rectangle using the available sticks.
Refer sample input and output for formatting specifications.
Sample Input 1:
5
1 2 3 1 2
Sample Output 1:
2
Sample Input 2:
4
1 2 2 3
Sample Output 2:
-1
Stella was given a total of N sticks. Length of i-th stick is Ai. Bob insists Stella to choose any four sticks and to make a rectangle with those sticks as its sides. Bob warns Stella not to break any of the sticks, she has to use sticks as a whole.
Also, Bob wants that the rectangle formed should have the maximum possible area among all the rectangles that Stella can make. Stella takes this challenge up and overcomes it. You have to help her know whether it is even possible to create a rectangle. If yes, then tell the maximum possible area of rectangle.
Input Format:
The first line of the input contains a single integer N denoting the number of sticks.
The second line of each test case contains N space-separated integers A1, A2, ...,AN denoting the lengths of sticks.
Output Format:
Output a single line containing an integer representing the maximum possible area for rectangle or output -1, if it's impossible to form any rectangle using the available sticks.
Refer sample input and output for formatting specifications.
Sample Input 1:
5
1 2 3 1 2
Sample Output 1:
2
Sample Input 2:
4
1 2 2 3
Sample Output 2:
-1
Solution:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(arr[i]<arr[j])
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
int l=0,b=0,count=0;
for(int i=0;i<n-1;i++) //3221
{
if(arr[i]==arr[i+1])
{
i++;
++count;
if(count==1)
l=arr[i];
else if(count==2)
{
b=arr[i];
}
}
if(count==2)
{
break;
}
}
int area=l*b;
if(area>0)
System.out.println(area);
else
System.out.println("-1");
}
}
Comments
Post a Comment