Professional Documents
Culture Documents
Tricky Problem-Case Study by M.S.Bennet Praba, Ap/Cse, Srmist Problem Statement - I
Tricky Problem-Case Study by M.S.Bennet Praba, Ap/Cse, Srmist Problem Statement - I
Tricky Problem-Case Study by M.S.Bennet Praba, Ap/Cse, Srmist Problem Statement - I
By M.S.Bennet Praba,
AP/CSE, SRMIST
PROBLEM STATEMENT - I
Praveen has an upgraded version of the popular game Tower of Hanoi. Thisgame has n disks and 4 rods
that are ordered by ascending size. Praveen has already made a few moves following the general rules of
the Hanoi game. The problem statementfor the Praveen’s Hanoi is to determine the minimum number of
moves that he needs to restore the tower to its original state with all disks on rod 1.
Note: Praveen’s rods are numbered from 1 to 4. The radius of a disk is its index in the input array, so
disk 1 is the smallest disk with a radius of 1, and disk n is the largest with a radius of n.
Example:
posts = [4,3,2,1]
In this case, the disks are arranged from large to small across the four rods. The largest disk, disk4, is
already on rod 1, so move disks 3,2 and1 to rod 1 , in that order. It takes moves to reset the game.
posts = [4,2,2,1]
In this case, the largest disk, that is thedisk 4 is already on rod 1. Disk 3 is on rod 2 and must be below
disk 2. Move disk 2 to rod 3, disk 3 to rod 1 and disk 2 to rod 1. Now move disk 1 to rod 1. It
takes 3 moves to reset the game.
Input Format
- The first line contains a single integer n which is the number of disks.
- The second line contains n space-separated integers, where the ith integer is the index of the
rod where the disk with diameter i is located.
Description
- The game of Hanoi has the following parameters:
int posts[n] – posts[i]is the location of disk with radius i.
Output Format
- The minimum moves to reset the game to its initial state.
Sample Input
3
141
Sample Output
3
Explanation
Here the first input is the size of posts which is n = 3.
And the posts = [1,4,1]. The solution is 3 moves that are enough to build the tower.
Solution:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
currentTests = nextTests;
allTests.addAll(currentTests);
currentMoves++;
}
System.out.println(currentMoves);
}
}
Screenshot
PROBLEM STATEMENT - II
Shravan works at a clothing store. He has a large pile of shirts that he must pair by colour for sale.
Given an array of integers representing the colour of each shirt, determine how many pairs of shirts
with matching colours there are.
Example
Thereis n=7 shirts with colours ar= [1,2,1,2,1,3,2]. There is a pair of colours 1 and 2. There are three
odd shirts left, one of each colour. The numberof pairs is 2.
Description
- Write the solution function that must return an integer representing the number of
matching pairs of shirts that are available.
Input Format
- The first line contains an integer n, the number of shirts represented in ar. The second line
contains n space-separated integers describing the colours ar[i] of the shirts in the pile.
Output Format
- Return the total number of matching pairs of shirts that Shravan can sell.
Sample Input
9
10 20 20 10 10 30 50 10 20
Sample Output
3
Solution:
Screenshot