-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMani_NicolasGasPuzzle.java
More file actions
88 lines (72 loc) · 2.43 KB
/
Mani_NicolasGasPuzzle.java
File metadata and controls
88 lines (72 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.learing.main;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class Mani_NicolasGasPuzzle {
public static void main(String[] args) {
System.out.println(">> Enter the Number of Tests : ");
Scanner sc= new Scanner (System.in);
ArrayList<Integer> result = new ArrayList<>();
int numTests = sc.nextInt();
for(int i =0;i<numTests;i++) {
HashMap<Integer,Integer> map = new HashMap<>();
Map<Integer,Integer> sortedmap = new HashMap<>();
System.out.println(">> Enter N value :");
int N = sc.nextInt();
System.out.println(">> Enter P value :");
int P = sc.nextInt();
System.out.println(">> Enter K value :");
int K = sc.nextInt();
map=gettheValIndices(N,K);
sortedmap=sortByValue(map);
// System.out.println(map);
// System.out.println(">> Sorted Map : "+ sortedmap);
Set<Integer> aa=sortedmap.keySet();
int days=1;
for(Integer a: aa) {
if(a==P) {
// System.out.println(">> NUmber of days : "+days);
result.add(days);
}
days++;
}
}
for(Integer ab:result) {
System.out.println(ab);
}
}
private static HashMap<Integer, Integer> gettheValIndices(int n,int k) {
HashMap<Integer,Integer> map=new HashMap<>();
for(int i=0;i<n;i++) {
int val=i%k;
map.put(i, val);
}
// System.out.println(">> Unsorted Map : " + map);
// System.out.println(">> Sorted Map : " + map);
return map;
}
public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm)
{
List<Map.Entry<Integer, Integer> > list =
new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet());
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() {
public int compare(Map.Entry<Integer, Integer> o1,
Map.Entry<Integer, Integer> o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
}