Alice Apple Tree
Assume the vertex is (r,r),(r,-r),(-r,r) and (-r,-r).
Split the apples into 4 rectangles,
each rectangle is r * (r + 1).
Put two blue matrix together
and merge apples tiles at the same
position,
each tile become r + r + 1.
The total apples in blue is,
r * (r + 1) * (r + r + 1)
Put two green matrix together
and merge apples tiles at the same
position,
each tile become r + r + 1.
The total apples in green is,
r * (r + 1) * (r + r + 1)
The total apples is:
r * r * r * 4 + r * r * 6 + r * 2.
1. import java.util.*;
2. public class AliceAppleTree {
3. public static void main(String[]
args) {
4. Scanner sc=new
Scanner(System.in);
5. int apple=sc.nextInt();
6. int cnt=0,sum=0;
7. while(sum<apple){
8. cnt++;
9. sum+=(12*cnt*cnt);
10.
11. }
12. System.out.println((8*(cnt)));
13. }
14.}
Toggle The Switch
Problem statement
There are 100 closed doors. A cage holding 100 monkeys is placed nearby. Every monkey that
visits a door either opens a closed door or closes an open door. The first monkey that is let out
of the cage visits and opens all the hundred doors. The second monkey that is released visits
doors of the order 2, 4, 6, 8, 10…. . The third monkey released visits doors 3, 6, 9,12, 15……,
and so on.
After all the monkeys from the cage are released and have opened or closed at least one door,
how many doors are left open?
Approach
Let us understand how to solve this problem by observing the activity of doors 13, 50,
and 16. 13 is a prime number, 50 is a composite number, and 16 is a perfect square.
1. import java.util.*;
2. public class Toggle {
1. for(i=1;i<=n;i++)
3. public static void main(String[]
2. {
args) {
3. if(b[i]==true)
4. Scanner sc = new
4. {
Scanner(System.in);
5. c++;
5. int n = sc.nextInt();
6. }
6. boolean b[]= new boolean[n+1];
7. else{
7.
8. int i,j,c=0,o=0;
8. o++;
9. }
for(i=1;i<=n;i++)
10. }
9. {
11. System.out.println("No Of
10. for(j=i;j*i<=n;j++)
Doors open"+c);
11. {
12. System.out.println("No Of
12. if(b[j]==false)
13. { 13.Doors closed"+o);
14. b[j]=true;
}
15. }
14.}
16. else{
17. b[j]=false;
18. }
19. }
20. }
THANK YOU