- 145.5k
- 22
- 190
- 478
Converting long to int efficiently HackerEarth Girlfriend's Demand challenge, solved two ways
I solved the Girlfriend's Demand challenge on HackerEarth . In summary:
Like most of the girlfriends, Ashima when asks for something, won’t stop until she gets that. The way she gets that is by keep on repeating the same things again and again. Like if she wants chocolate, she will just keep on repeating "chocolate" again and again.
I have decided to answer to her demands as "Yes" or "No" by not delaying a lot. Otherwise, there would be a lot of repercussions. So, randomly at certain intervals, I just answer with "Yes" or "No" using the following rule, I will just select two integers a and b, if the element at the position a is same as the element as position b in the non-ending chant by Ashima, I will speak "Yes", otherwise say "No".
Your job is to find my side of the conversation given the name of the demand Ashima has and the random integers I picked.
Input: First line of the input contains a string S, the name of the item she is demandingS. Next line contains an integer QQ, the number of pairs of integers that used to say "Yes" or "No" to her. These pairs are given in orderqueries. Next Q line, each Each of the next Q lines contains a test case consisting of 2 integers, aa and bb. (1-based indexing)
Output: ForIf S is repeated infinitely, would string positions a and b contain the same character (using 1-based indexing)? For each query, print "Yes" or "No" as described above.
Constraints:
\1ドル \le |S| \le 10^{51} \le Q \le 10^5 1 \le a, b \le 10^{18}\$
- 1 ≤ |S| ≤ 105
- 1 ≤ Q ≤ 105
- 1 ≤ a, b ≤ 1018
Sample Input:
vgxgp 3 2 4 2 5 7 14
Sample Output:
Yes No Yes
My first solution:
Converting long to int efficiently
Like most of the girlfriends, Ashima when asks for something, won’t stop until she gets that. The way she gets that is by keep on repeating the same things again and again. Like if she wants chocolate, she will just keep on repeating "chocolate" again and again.
I have decided to answer to her demands as "Yes" or "No" by not delaying a lot. Otherwise, there would be a lot of repercussions. So, randomly at certain intervals, I just answer with "Yes" or "No" using the following rule, I will just select two integers a and b, if the element at the position a is same as the element as position b in the non-ending chant by Ashima, I will speak "Yes", otherwise say "No".
Your job is to find my side of the conversation given the name of the demand Ashima has and the random integers I picked.
Input: First line of the input contains a string S, the name of the item she is demanding. Next line contains an integer Q, the number of pairs of integers that used to say "Yes" or "No" to her. These pairs are given in order. Next Q line, each contains 2 integers, a and b. (1-based indexing)
Output: For each query, print "Yes" or "No" as described above.
Constraints:
\1ドル \le |S| \le 10^{51} \le Q \le 10^5 1 \le a, b \le 10^{18}\$
Sample Input:
vgxgp 3 2 4 2 5 7 14
Sample Output:
Yes No Yes
HackerEarth Girlfriend's Demand challenge, solved two ways
I solved the Girlfriend's Demand challenge on HackerEarth . In summary:
Input: First line of the input contains a string S. Next line contains an integer Q, the number of queries. Each of the next Q lines contains a test case consisting of 2 integers, a and b.
Output: If S is repeated infinitely, would string positions a and b contain the same character (using 1-based indexing)? For each query, print "Yes" or "No".
Constraints:
- 1 ≤ |S| ≤ 105
- 1 ≤ Q ≤ 105
- 1 ≤ a, b ≤ 1018
Sample Input:
vgxgp 3 2 4 2 5 7 14
Sample Output:
Yes No Yes
My first solution:
Converting long to int conversion efficiently?
Like most of the girlfriends, Ashima when asks for something, won’t stop until she gets that. The way she gets that is by keep on repeating the same things again and again. Like if she wants chocolate, she will just keep on repeating "chocolate" again and again.
I have decided to answer to her demands as "Yes" or "No" by not delaying a lot. Otherwise, there would be a lot of repercussions. So, randomly at certain intervals, I just answer with "Yes" or "No" using the following rule, I will just select two integers a and b, if the element at the position a is same as the element as position b in the non-ending chant by Ashima, I will speak "Yes", otherwise say "No".
Your job is to find my side of the conversation given the name of the demand Ashima has and the random integers I picked.
Input: First line of the input contains a string S, the name of the item she is demanding. Next line contains an integer Q, the number of pairs of integers that used to say "Yes" or "No" to her. These pairs are given in order. Next Q line, each contains 2 integers, a and b. (1-based indexing)
Output:
Output: For each query, print "Yes" or "No" as described above.
Constraints: 1 ≤ |S| ≤ 10^5 1 ≤ Q ≤ 10^5 1 ≤ a, b ≤ 10^18
Constraints:Sample Input vgxgp 3 2 4 2 5 7 14
\1ドル \le |S| \le 10^{51} \le Q \le 10^5 1 \le a, b \le 10^{18}\$
Sample Input:
vgxgp 3 2 4 2 5 7 14
Output Yes No Yes
Sample Output:
Yes No Yes
Here are the codes that i have written
public static void main(String args[] ) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String item=br.readLine();
StringBuffer sb=new StringBuffer();
int len = item.length();
int inputs = Integer.parseInt(br.readLine());
while(inputs-->0)
{
String strarr[] =null;
strarr = br.readLine().split(" ");
long a=Long.parseLong(strarr[0]);
long b=Long.parseLong(strarr[1]);
a=a-1;
b=b-1;
a=a%len;
b=b%len;
if(item.charAt((int)a)==item.charAt((int)b))
sb.append("Yes\n");
else
sb.append("No\n");
}
System.out.println(sb);
}
public static void main(String args[] ) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String item=br.readLine();
StringBuffer sb=new StringBuffer();
int len = item.length();
int inputs = Integer.parseInt(br.readLine());
while(inputs-->0)
{
String strarr[] =null;
strarr = br.readLine().split(" ");
long a=Long.parseLong(strarr[0]);
long b=Long.parseLong(strarr[1]);
a=a-1;
b=b-1;
a=a%len;
b=b%len;
if(item.charAt((int)a)==item.charAt((int)b))
sb.append("Yes\n");
else
sb.append("No\n");
}
System.out.println(sb);
}
While this gave me an output in almost 5.2954 sec for various inputs .seconds All INPUTS LINKSfor various inputs
while, when iI tried the same inputs on this version of the code that iI wrote:
public static void main(String args[] ) throws Exception {
BufferedReader reader= new BufferedReader(new InputStreamReader(System.in));
String demanded=reader.readLine();
int length = demanded.length();
int T=Integer.parseInt(reader.readLine());
for(int i = 0;i < T; i++){
String[] pairedAAndB=reader.readLine().split(" ");
long a = ((Long.parseLong(pairedAAndB[0]) - 1) % length);
long b = ((Long.parseLong(pairedAAndB[1]) -1) %length);
System.out.println((demanded.charAt((int)a)==demanded.charAt((int)b)) ? "Yes" : "No");
}
}
public static void main(String args[] ) throws Exception {
BufferedReader reader= new BufferedReader(new InputStreamReader(System.in));
String demanded=reader.readLine();
int length = demanded.length();
int T=Integer.parseInt(reader.readLine());
for(int i = 0;i < T; i++){
String[] pairedAAndB=reader.readLine().split(" ");
long a = ((Long.parseLong(pairedAAndB[0]) - 1) % length);
long b = ((Long.parseLong(pairedAAndB[1]) -1) %length);
System.out.println((demanded.charAt((int)a)==demanded.charAt((int)b)) ? "Yes" : "No");
}
}
It took around 15.7819 (sec) seconds. What areis the best way to convert longlong
to int optimally .int
optimally? What i'mI'm not able to understand Whyis why the second code snippet is taking so much of time when the second version is almost similar or there. There must be something about the second version that i'mI'm not able to getunderstand.
long to int conversion efficiently?
Like most of the girlfriends, Ashima when asks for something, won’t stop until she gets that. The way she gets that is by keep on repeating the same things again and again. Like if she wants chocolate, she will just keep on repeating "chocolate" again and again.
I have decided to answer to her demands as "Yes" or "No" by not delaying a lot. Otherwise, there would be a lot of repercussions. So, randomly at certain intervals, I just answer with "Yes" or "No" using the following rule, I will just select two integers a and b, if the element at the position a is same as the element as position b in the non-ending chant by Ashima, I will speak "Yes", otherwise say "No".
Your job is to find my side of the conversation given the name of the demand Ashima has and the random integers I picked.
Input: First line of the input contains a string S, the name of the item she is demanding. Next line contains an integer Q, the number of pairs of integers that used to say "Yes" or "No" to her. These pairs are given in order. Next Q line, each contains 2 integers, a and b. (1-based indexing)
Output:
For each query, print "Yes" or "No" as described above.
Constraints: 1 ≤ |S| ≤ 10^5 1 ≤ Q ≤ 10^5 1 ≤ a, b ≤ 10^18
Sample Input vgxgp 3 2 4 2 5 7 14
Sample
Output Yes No Yes
Here are the codes that i have written
public static void main(String args[] ) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String item=br.readLine();
StringBuffer sb=new StringBuffer();
int len = item.length();
int inputs = Integer.parseInt(br.readLine());
while(inputs-->0)
{
String strarr[] =null;
strarr = br.readLine().split(" ");
long a=Long.parseLong(strarr[0]);
long b=Long.parseLong(strarr[1]);
a=a-1;
b=b-1;
a=a%len;
b=b%len;
if(item.charAt((int)a)==item.charAt((int)b))
sb.append("Yes\n");
else
sb.append("No\n");
}
System.out.println(sb);
}
While this gave me an output in almost 5.2954 sec for various inputs . All INPUTS LINKS
while when i tried the same inputs on this version of code that i wrote
public static void main(String args[] ) throws Exception {
BufferedReader reader= new BufferedReader(new InputStreamReader(System.in));
String demanded=reader.readLine();
int length = demanded.length();
int T=Integer.parseInt(reader.readLine());
for(int i = 0;i < T; i++){
String[] pairedAAndB=reader.readLine().split(" ");
long a = ((Long.parseLong(pairedAAndB[0]) - 1) % length);
long b = ((Long.parseLong(pairedAAndB[1]) -1) %length);
System.out.println((demanded.charAt((int)a)==demanded.charAt((int)b)) ? "Yes" : "No");
}
}
It took around 15.7819 (sec) . What are the best way to convert long to int optimally . What i'm not able to understand Why second code is taking so much of time when the second version is almost similar or there must be something about the second version that i'm not able to get
Converting long to int efficiently
Like most of the girlfriends, Ashima when asks for something, won’t stop until she gets that. The way she gets that is by keep on repeating the same things again and again. Like if she wants chocolate, she will just keep on repeating "chocolate" again and again.
I have decided to answer to her demands as "Yes" or "No" by not delaying a lot. Otherwise, there would be a lot of repercussions. So, randomly at certain intervals, I just answer with "Yes" or "No" using the following rule, I will just select two integers a and b, if the element at the position a is same as the element as position b in the non-ending chant by Ashima, I will speak "Yes", otherwise say "No".
Your job is to find my side of the conversation given the name of the demand Ashima has and the random integers I picked.
Input: First line of the input contains a string S, the name of the item she is demanding. Next line contains an integer Q, the number of pairs of integers that used to say "Yes" or "No" to her. These pairs are given in order. Next Q line, each contains 2 integers, a and b. (1-based indexing)
Output: For each query, print "Yes" or "No" as described above.
Constraints:
\1ドル \le |S| \le 10^{51} \le Q \le 10^5 1 \le a, b \le 10^{18}\$
Sample Input:
vgxgp 3 2 4 2 5 7 14
Sample Output:
Yes No Yes
public static void main(String args[] ) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String item=br.readLine();
StringBuffer sb=new StringBuffer();
int len = item.length();
int inputs = Integer.parseInt(br.readLine());
while(inputs-->0)
{
String strarr[] =null;
strarr = br.readLine().split(" ");
long a=Long.parseLong(strarr[0]);
long b=Long.parseLong(strarr[1]);
a=a-1;
b=b-1;
a=a%len;
b=b%len;
if(item.charAt((int)a)==item.charAt((int)b))
sb.append("Yes\n");
else
sb.append("No\n");
}
System.out.println(sb);
}
While this gave me an output in almost 5.2954 seconds for various inputs , when I tried the same inputs on this version of the code that I wrote:
public static void main(String args[] ) throws Exception {
BufferedReader reader= new BufferedReader(new InputStreamReader(System.in));
String demanded=reader.readLine();
int length = demanded.length();
int T=Integer.parseInt(reader.readLine());
for(int i = 0;i < T; i++){
String[] pairedAAndB=reader.readLine().split(" ");
long a = ((Long.parseLong(pairedAAndB[0]) - 1) % length);
long b = ((Long.parseLong(pairedAAndB[1]) -1) %length);
System.out.println((demanded.charAt((int)a)==demanded.charAt((int)b)) ? "Yes" : "No");
}
}
It took around 15.7819 seconds. What is the best way to convert long
to int
optimally? What I'm not able to understand is why the second code snippet is taking so much time when the second version is almost similar. There must be something about the second version that I'm not able to understand.