Deepak Agrawal
BAN USER- 0of 0 votes
AnswersFind the index when slow and fast pointer meet in terms of n (length of list before cycle) and p ( length of loop in linked list).
- Deepak Agrawal in India
Let me meeting index is q then we should be able to find value of q when we pass n& p , there shouldn't be any extra variable.| Report Duplicate | Flag | PURGE
Google Software Engineer Algorithm
public class Solution {
private static int[] bases = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9,
5, 4, 1 };
private static HashMap<Integer, String> map = new HashMap<Integer, String>();
private static void setup() {
map.put(1, "I");
map.put(4, "IV");
map.put(5, "V");
map.put(9, "IX");
map.put(10, "X");
map.put(40, "XL");
map.put(50, "L");
map.put(90, "XC");
map.put(100, "C");
map.put(400, "CD");
map.put(500, "D");
map.put(900, "CM");
map.put(1000, "M");
}
public String intToRoman(int num) {
setup();
String result = new String();
for (int i : bases) {
while (num >= i) {
result += map.get(i);
num -= i;
}
}
return result;
}
}
Using srand(time(null)) we can generate random numbers along with the usage of system clock to assure every time a random number when a code is compiled.
- Deepak Agrawal April 11, 2014I have one approach which runs in o(n) without extra space but valid only when you know the upper limit of number.
Suppose you are given that the number lies within 1-n where n is the size of array then you can easily find in o(n) by iterating a loop ar[abs(ar[i])%n]+=n;
Ex: ar1[]={2,3,1,4} then ar1 after loop becomes { 6,7,5,8}
ar2[] ={1,4,2,3} then ar2 becomes { 5,6,7,8} so in one loop you can easily count the number of times each index comes by diving by n and then checking elements in both arrays.
Using an interface is necessary when you want that the classes must implement some of the prerequired methods.
Ex. Employee inherits person class so employee should have eat function which is defined in person class so instead of making abstract class we make an interface person so that whenever any class in inheriting the class it will sign the contract to define eat person.
And ofcouse it supports multiple inheritance in java using interfaces.
And you can also support run time polymorphism using interfaces because the person interface derives manager and employee class so the reference of person can store the instance of manager as well as the instance of employee.
So using interface has many advantages.
#define f(x) #x
- Deepak Agrawal April 11, 2014void fun(string s) {
cout<<s;
}
int main()
{
fun(f(21));// prints 21
fun(f(DE));//prints DE
return 0;
}