bjain
BAN USER- 0of 0 votes
AnswersDesign a File system for windows or linux machine. Use OO concepts.
- bjain in United States| Report Duplicate | Flag | PURGE
Amazon
- 2 Answers Order of code
public static void main(String[] args) {
- bjain September 20, 2014
int i, j, k, count, n;
n = 100; count = 0;
for (i = 0; i < n; i++){
for (j = 0; j < i; j++){
for (k = 0; k < j; k++){
count = count + 1;
} } }
System.out.println("count " + count); }
What will be the order of execution of the above code?| Flag | PURGE
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
public class abcdQuestion {
public static void main(String[] args) {
printString(28);
}
public static void printString(int k) {
if (k <= 26) {
System.out.println(Character.toString((char) (96 + k)));
return;
}
HashMap<Integer, String> hp = new HashMap<Integer, String>();
HashMap<Integer, String> temp = new HashMap<Integer, String>();
for (int i = 1; i <= 26; i++) {
hp.put(i, Character.toString((char) (i + 96)));
}
int count = 27;
int l = 1;
while (count <= k) {
String s = hp.get(l);
Iterator<Entry<Integer, String>> it = hp.entrySet().iterator();
while (it.hasNext() && count <= k) {
String d = s + it.next().getValue();
temp.put(count, d);
count++;
}
hp.putAll(temp);
if ((count - 1) == k) {
System.out.println(hp.get(k));
return;
}
l = l + 1;
}
}
}
public class MaxSumConseqNosArray {
public static void main(String[] args) {
int max_ending_here = 0;
int max_so_far = 0;
int start = 1, end = 0;
int arr[] = { 5, 3, -20, 7, 9 };
for (int i = 0; i < arr.length; i++) {
max_ending_here = max_ending_here + arr[i];
if (max_ending_here < 0) {
max_ending_here = 0;
end = i;
}
/*
* Do not compare for all elements. Compare only when
* max_ending_here > 0
*/
if (max_so_far < max_ending_here) {
max_so_far = max_ending_here;
} else {
start = i;
}
System.out.println(max_so_far);
}
System.out.println(max_so_far);
System.out.println("strat: nad end: " + start + " " + end);
}
}
in case of all negative numbers we can return the largest number among them,that would be the largest sum. For this initially we have to check if the given array contains all negative numbers.
In case of mixed +ve and -ve nos. thi will work
public class MaxSumConseqNosArray {
public static void main(String[] args) {
int max_ending_here = 0;
int max_so_far = 0;
int start = 1, end = 0;
int arr[] = { 5, 3, -20, 7, 9 };
for (int i = 0; i < arr.length; i++) {
max_ending_here = max_ending_here + arr[i];
if (max_ending_here < 0) {
max_ending_here = 0;
end = i;
}
/*
* Do not compare for all elements. Compare only when
* max_ending_here > 0
*/
if (max_so_far < max_ending_here) {
max_so_far = max_ending_here;
} else {
start = i;
}
System.out.println(max_so_far);
}
System.out.println(max_so_far);
System.out.println("strat: nad end: " + start + " " + end);
}
}
@gingd7 nice soln..
- bjain May 17, 2012