Tricky Problem-Case Study by M.S.Bennet Praba, Ap/Cse, Srmist Problem Statement - I

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

TRICKY PROBLEM-CASE STUDY

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.*;

public class Solution {

public static int[] readIntArray3(Scanner in, int size) {


int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = in.nextInt();
}
return arr;
}

public static void main(String[] args) throws Exception {


Scanner in = new Scanner(System.in);
int cases = 1;//in.nextInt();
for (int testcase = 0; testcase < cases; testcase++) {
int n = in.nextInt();
int[] arr = readIntArray3(in, n);
genahanoi(n, arr);
}
}

public static void genahanoi(int n, int[] locs) {


int[] powers = new int[]{1,4,16,64,256,1024,4096,16384,65536,26
2144};
int score = 0;
for (int i = 0; i < n; i++) {
score += powers[i]*(locs[i]-1);
}
Set<Integer> allTests = new HashSet();
Set<Integer> currentTests = new HashSet();
currentTests.add(score);
allTests.add(score);
int currentMoves = 0;
while (!currentTests.contains(0)) {
Set<Integer> nextTests = new HashSet();
for (int test : currentTests) {
int[] tops = new int[]{-1,-1,-1,-1};
for (int i = n-1; i >=0; i--) {
int loc = (test>>(2*i))%4;
tops[loc] = i;
}
for (int j = 0; j < 4; j++) {
if (tops[j] >= 0) {
for (int k = 0; k < 4; k++) {
if ( k != j) {
if (tops[k] == -
1 || tops[k] > tops[j]) {
int newTest = test -
(j<<(2*tops[j])) + (k<<(2*tops[j]));
if (!allTests.contains(newTest)) {
nextTests.add(newTest);
} else {
}
}
}
}
}
}
}

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.

This program has the following parameter(s):


• n: the number of shirts in the pile
• ar: the colours of each shirt

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:

public class Solution {


static int shravanShirts(int n, int[] ar) {
int pairs = 0;
int frequencyArray[] = new int[ar.length];
int frequencyTemp = -1;
for (int i = 0; i < ar.length; i++) {
int count = 1;
for (int j = i + 1; j < ar.length; j++) {
if (ar[i] == ar[j]) {
count++;
frequencyArray[j] = frequencyTemp;
}
}
if (frequencyArray[i] != frequencyTemp) {
frequencyArray[i] = count;
}
}

for (int i = 0; i < frequencyArray.length; i++) {


if (frequencyArray[i] != frequencyTemp) {
int divide = frequencyArray[i] / 2;
pairs += divide;
}
}
return pairs;
}

public static void main(String[] args) {


Scanner in = new Scanner(System.in);
int num = in.nextInt();
int[] ar = new int[num];
for (int i = 0; i < num; i++) {
ar[i] = in.nextInt();
}
System.out.println(shravanShirts(num, ar));
}

Screenshot

You might also like