forked from rootusercop/IMP-Interview-Questions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VERY VERY IMP THING IN PROGRAMMING
55 lines (47 loc) · 1.48 KB
/
VERY VERY IMP THING IN PROGRAMMING
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
Question: https://www.hackerrank.com/challenges/halloween-party
Ans:
When we store the answer in different datatype, always remember to typecast it
WRONG ANS:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();
for(int i=0;i<n;i++){
int k = in.nextInt();
long ans = 0;
ans = (k/2)*(k-(k/2));
System.out.println(ans);
if(i!=n-1)
in.nextLine();
}
}
}
CORRECT ANS:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();
for(int i=0;i<n;i++){
int k = in.nextInt();
long ans = 0;
ans = (long)(k/2)*(k-(k/2)); // When we store the answer in different datatype, always remember to typecast it
System.out.println(ans);
if(i!=n-1)
in.nextLine();
}
}
}