Sam
BAN USER- 1of 1 vote
AnswersGiven these 4 commands :
INCREMENT: READ V WRITE V + 1 ZERO WRITE 0 ASSIGN : = LOOP : FOR <integer> = <start> to <finish> do ... END FOR
Define these 4 functions: Decrement, Divide, test If equal (pseudo code).
- Sam in United States| Report Duplicate | Flag | PURGE
Software Engineer / Developer Algorithm
It is a very good answer. Just a comment for improvement: You don't have to assign 0 to remaining position, their default value is already zero. So:
private static int[] sort(int[] arr){
int[] res = new int[arr.length];
int count = arr.length - 1;
for(int i = 0; i < arr.length; i++){
if(arr[i] != 0)
res[count--] = arr[i];
}
return res;
}
Integer array is converted to a String, i.e:
dup = "1,2,7,9,9,5,4,9";
then it is converted to String array:
String[] dupArr = dup.split(",");//[1, 2, 7, 9, 9, 5, 4, 9]
Then all occurrences of a number are removed then the number is added to the string:
dup = dup.replace(dupArr[i], "");
dup += a[i] + ",";
Finally, the integer array is reconstructed without duplicate.
- Sam April 07, 2013My java solution using String in O(n):
import java.util.Arrays;
public class Remove_Dup_No_Collection {
public static void main(String[] args) {
int[] a = {1,2,7, 9, 9, 5, 4, 9};
System.out.println(Arrays.toString(removeDup(a)));
}
private static int[] removeDup(int[] a) {
String dup = "";
for (int i = 0; i < a.length; i++) {
dup += "" + a[i] + ",";
}
//removing last ","
dup = dup.substring(0, dup.length()-1);
String[] dupArr = dup.split(",");
for (int i = 0; i < dupArr.length; i++) {
dup = dup.replace(dupArr[i], "");
dup += a[i] + ",";
}
String[] ans = dup.split(",");
int count = 0;
for (int i = 0; i < ans.length; i++) {
if(!ans[i].equals(""))
count++;
}
int[] newArr = new int[count];
count = 0;
for (int i = 0; i < ans.length; i++) {
if(!ans[i].equals(""))
newArr[count++] = Integer.parseInt(ans[i]);
}
return newArr;
}
}
Output:
[1, 2, 7, 5, 4, 9]
My java solution in O(n):
import java.util.*;
public class Array_Odd_Occ {
public static void main(String[] args) {
int a[]={2,3,2,3,3,4,5,4,2,2,5,6};
findNumbers(a);
}
private static void findNumbers(int [] a) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < a.length; i++) {
Integer freq = map.get(a[i]);
map.put(a[i], (freq == null) ? 1 : freq + 1);
}
for(Map.Entry<Integer, Integer> nums : map.entrySet()) {
if(nums.getValue() % 2 != 0)
System.out.println(nums.getKey());
}
}
}
Output:
3 6
This is how I did it:
import java.util.Arrays;
public class Two_Sub_Sorted_Array {
public static void main(String[] args) {
int[] arr = {1, 4, 5, 7, 8, 9, 2, 3, 6, 10, 11};
sort(arr);
}
public static void sort(int[] a) {
int sec = 0;//first index of second array
for (int i = 0; i + 1 < a.length; i++) {
if(a[i] > a[i + 1]) {
sec = i + 1;
break;
}
}
//first array
int[] a1 = Arrays.copyOfRange(a, 0, sec);
//second array
int[] a2 = Arrays.copyOfRange(a, sec, a.length);
System.out.println("a1: " + Arrays.toString(a1));
System.out.println("a2: " + Arrays.toString(a2));
//result array
int[] res = new int[a.length];
int c1 = 0, c2 = 0, limit = 0;
boolean arr1 = false;
//sorting
for (int i = 0; i < res.length; i++) {
if(a1[c1] < a2[c2]) {
res[i] = a1[c1++];
} else if(a1[c1] > a2[c2]) {
res[i] = a2[c2++];
} else if (a1[c1] == a2[c2]) {
res[i] = a1[c1++];
res[++i] = a2[c2++];
}
if(c1 == a1.length){
limit = i + 1;
arr1 = true;
break;
} else if (c2 == a2.length) {
limit = i + 1;
break;
}
}
if(arr1){
for (int i = limit; i < res.length; i++) {
res[i] = a2[c2++];
}
} else {
for (int i = limit; i < res.length; i++) {
res[i] = a1[c1++];
}
}
//printing the result
System.out.println(Arrays.toString(res));
}
}
Output:
a1: [1, 4, 5, 7, 8, 9]
a2: [2, 3, 6, 10, 11]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
So your version is 1.4, so this should work for you:
public class SubStringCount {
public static void main(String[] args) {
System.out.println(count("aaaaa", "aaa"));
}
public static int count(String s, String sub){
int counter = 0;
String temp;
char [] c = s.toCharArray();
for (int i = 0; i + sub.length() <= s.length(); i++) {
// Copies the specified range of the specified array into a new array
//using string constructor, char array in converted into string
temp = new String(copyOfRange(c, i, i+sub.length()));
if(temp.equals(sub))
counter++;
}
return counter;
}
public static char[] copyOfRange(char[] original, int from, int to) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
char[] copy = new char[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}
}
Let me know.
- Sam March 07, 2013Here is my solution, it does not use brute -force nor substring method:
import java.util.Arrays;
public class SubStringCount {
public static void main(String[] args) {
System.out.println(count("abcdefghcde", "cde"));
}
public static int count(String s, String sub){
int counter = 0;
String temp;
char [] c = s.toCharArray();
for (int i = 0; i + sub.length() <= s.length(); i++) {
// Copies the specified range of the specified array into a new array
//then, using string constructor, char array is converted into string
temp = new String(Arrays.copyOfRange(c, i, i+sub.length()));
if(temp.equals(sub))
counter++;
}
return counter;
}
}
Edited the question, Thanks.
- Sam May 22, 2013