vrajendra.singh.mandloi
BAN USER- -1of 3 votes
AnswersQuest: create a Random number generator without using the java in build Random class?
- vrajendra.singh.mandloi in India
public class GenerateRandomNumbers {
public static int seed = 10;
public void generateInt(int num){
seed = num;
for(int i=1;i<num;i++){
seed = ((int)System.nanoTime()%(i));
if(seed<=0)
seed = ((int)System.currentTimeMillis()%i);
System.out.println(seed);
}
}
public static void main(String[] args) {
new GenerateRandomNumbers().generateInt(20);
}
}
its a class to generate random numbers but the problem is It always starts with 0...!
Can someone add some more to this code.??| Report Duplicate | Flag | PURGE
Google Software Engineer / Developer Algorithm
The best possible recursive solution which i feel is possible .. I tested the same for 1000000 size string
public static String reverseWords(String value){
if(value == null || value.length() == 0)
return "";
int index = value.indexOf(" ");
if(index <= 0)
return value;
return reverseWords(value.substring(index +1))+" "+value.substring(0,index);
}
One Good way to Find out Prime Number is
public static void primeNumbers(int n) {
boolean flag = true;
if(n%2 == 0) {
flag = false;
} else {
for(int i=n/2+1;i>1;i-=2){
if(n%i == 0) {
flag = false;
}
}
}
System.out.println("\n"+n+" is a Prime Number.. "+flag);
}
Please do let me know if someone has a better solution than this .. even recursion is great....
- vrajendra.singh.mandloi March 23, 2015That's a good one but very very Long... we can do the same using just two pointers and the entire list we can reverse after interval of K..
public Link<Integer> recursiveRecurse(Link<Integer> node, int k){
Link<Integer> q = node;
Link<Integer> r = null;
Link<Integer> s = null;
int count = 0;
while(q!=null && count < k) {
s = r;
r = q;
q = q.next;
r.next = s;
count++;
}
if(q != null) {
node.next = recursiveRecurse(q, k); // for joining the reversed part and the first node of the sublist.
}
return r;
}
Hi Guyzzz That'a A wonderful explanation for reverse a Linked List. but i am stuck with something else.. below is the function for reversion a list from a point till end... but it is looping infinite ...
can someone help me...
**now when i am printing the head it goes Infinite loop. It goes till the reversed part and then again the same List starts..**
current O/P 1->2->3->4->5->10->9->8->7->6->10->9->8->7.........
Desired O/P 1->2->3->4->5->10->9->8->7->6->null
public void reverseAfterK(int k){
Link<Integer> current = head;
for(int i=1;i<k;i++) {
if(current == null)
return;
current = current.next;
}
Link<Integer> newNode = reverseNodes(current);
current.next = newNode.next;
}
public Link<Integer> reverseNodes(Link<Integer> current){
Link<Integer> peek = current;
Link<Integer> rev = null;
while(peek!=null){
Link<Integer> temp = peek.next;
peek.next = rev;
rev = peek;
peek = temp;
}
return rev;
}
here is an optimum way to delete the Kth node from last..
public void deleteK(int k){
Link<Integer> current = head;
Link<Integer> previous = head;
for(int i=0;i<k;i++){
current = current.next;
if(current == null)
return;
}
while(current.next != null){
previous = previous.next;
current = current.next;
}
previous.next = previous.next.next;
}
Please do let me know in case of any improvement..
- vrajendra.singh.mandloi October 06, 2014One Possible Solution is this... Code is modifiable
public class InsertLinkBeforePointer {
Link<Integer> head = new Link<Integer>(10);
Link<Integer> pointer = null;
public InsertLinkBeforePointer(){
Link<Integer> l1 = new Link<Integer>(20);
Link<Integer> l2 = new Link<Integer>(30);
Link<Integer> l3 = new Link<Integer>(50);
Link<Integer> l4 = new Link<Integer>(60);
head.next = l1;
l1.next = l2;
l2.next = l3;
l3.next = l4;
pointer = l3;
}
public void insertbeforeAsc(int data){
Link<Integer> node = new Link<Integer>(data);
// add the new node where the pointer is pointing..
if(node.data < pointer.data) {
node.next = pointer.next;
pointer.next = node;
// Swap the data in both the nodes..
node.data = pointer.data;
pointer.data = data;
}
else {
while(pointer!=null)
{
if(pointer.data < data)
pointer = pointer.next;
else
{
node.next = pointer.next;
pointer.next = node;
// Swap the data in both the nodes..
node.data = pointer.data;
pointer.data = data;
break;
}
}
}
}
Merging two Linked List without using recursion.
public void sortList(){
Link<Integer> current = list1;
Link<Integer> buffer = list2;
// if list2 data is smaller than list1 exchange the current and buffer pointers
if(list2.data<list1.data)
{current = list2;
buffer = list1;
}
while(true){
if(buffer == null)
break;
if(current.data < buffer.data) {
current = current.next;
}
else {
Link<Integer> newNode = new Link<Integer>(buffer.data);
newNode.next = current.next;
current.next = newNode;
current = current.next;
buffer = buffer.next;
}
if(current.next==null) {
current.next = buffer;
break;
}
}
}
Changes are made in current pointer which references to list1.
- vrajendra.singh.mandloi October 05, 2014Sorry, every time the O/p is constant.
- vrajendra.singh.mandloi June 23, 2014please clarify .. what is N here.. and how can you clarify the complexity is O(logn)
- vrajendra.singh.mandloi December 28, 2013I simply did this by getting the two nodes from the get node function and then swapped the two. i got the answer correctly.
public LinkedListNode getNode(int index){
String str;
LinkedListNode prevnode = first.getNext();
for(int i=0;i<index-1;i++){
prevnode = prevnode.getNext(); // reaching the index node.
}
return prevnode;
}
// Swapping the Kth node from first and last alike.
public void specialSwap(int index){
LinkedListNode frontnode = first.getNext();
LinkedListNode lastnode = first.getNext();
frontnode = getNode(index); // obtain the first node
lastnode = getNode(size()-index+1); // obtain the last node
// Swap the Nodes values. no need of breaking the nodes.
String temp = frontnode.getName();
frontnode.setName(lastnode.getName());
lastnode.setName(temp);
}
why are we using Hashmap instead we can do the same using simple array work.. I know the approach using hashmap would be good.. or more memory efficient but can anybody explain how is it useful.. More memory efficient>>?
- vrajendra.singh.mandloi April 01, 2013public static char[] fillString(char[] str1, char[] str2){
int flag1 = 0;
int flag2 = 0;
for(int i=0;i<str1.length;i++){
System.out.println(str1[i]);
if(flag1>0){ // check the next in series
if(str1[i]=='?'){
if(flag1==str2.length)
flag1=0;
str1[i]= str2[flag1];
flag1++;
System.out.println("===="+flag1);
continue;
}
if(str1[i]==str2[flag1]){
flag1++;
System.out.println("===="+flag1);
continue;
}
}
if(str1[i]==str2[0]){ // check the first character
flag1=1;
System.out.println("===="+flag1);
continue;
}
if(str1[i]=='?'){ // if no match in string is present before
if(flag2==str2.length)
flag2=0;
str1[i]=str2[flag2];
System.out.println("+++++"+flag2);
flag2++;
continue;
}
}
return str1;
}
// Str1 = "ABAAMA?????MAZON??????????AMA??????????????????"
// Str2 = "AMAZON"
want to have your precious feedback, about the complexity and otherthings .. Please review it and let me know
String matcher[] = {"string search","java string search","manual c++ string search equals",
"java search code","c++ java code search" };
String[] pattern = "c++".split(" ");
for(String match : matcher){
for(int i=0;i<pattern.length;i++){
if(match.contains(pattern[i])){
if(i==pattern.length-1){
System.out.println(match);
}
continue;
}else
break;
}
}
I believe you might have asked the person for more quest.. like how exactly the array is.. meaning multiple occurrence of number is random or multiple occurrence will be together i.e. 33333311122225555. if this be the case then the answer is pretty straight forward.
- vrajendra.singh.mandloi July 24, 2016