nonameno
BAN USER- 0of 0 votes
AnswersGiven an array A [0, 1, 3, 4,9,5,7,6] and number N.
- nonameno in England
This means that the array consists of the numbers from 0 ... N. However, as you see, 8 is missing in A. Print the missing number.
Think about the case N = 10^6| Report Duplicate | Flag | PURGE
Bloomberg LP Software Engineer Intern Algorithm
This one simply compares two consecutive words and looks for the first letters of them, if they are different, changes the order of the alphabet according to them. If they are the same, looks for the next two letters in the same two words... Continues like this
#include <string>
#include <iostream>
using namespace std;
int indexOf(string main, char letter){
for (int a = 0; a < main.length();a++){
if (main[a]==letter)
return a;
}
return -1;
}
string alphabet(string * arr, int size){
string alph = "abcdefghijklmnopqrstuvwxyz";
for (int a = 0; a < size-1;a++){
int ind = 0;
while(ind < arr[a].length() && ind < arr[a+1].length() && arr[a][ind] == arr[a+1][ind]) ind++;
if (ind < arr[a].length() && ind < arr[a+1].length()){
int first = indexOf(alph, arr[a][ind]);
int last = indexOf(alph, arr[a+1][ind]);
if (first > last){
string temp = alph;
alph = temp.substr(0, last);
alph += temp.substr(last + 1, first - last);
alph += temp[last];
alph += temp.substr(first+1, temp.length());
}
}
}
return alph;
}
O(1) space complexity, O(n^2) time comlexity;
void removeDup(char array[], int &size){
for (int a = 0; a < size;a++){
char chr = array[a];
bool found = 0;
for (int b = a; b < size;b++){
if (found && array[b] == chr){
for (int c = b; c < size-1; array[c] = array[c+1], c++);
array[size-1] = '/0';
size--;
b--;
}
else if (array[b] == chr)
found = 1;
else continue;
}
}
}
I have found 240 slutions with the following code. I thought that for the first row (top) we have 4! possibility and according to this elements we have 2 possibility for the first two columns of second row. Then, we have another 2 possibility for the last two places of second row. Here is the important point!! We should choose first two elements of the third row according to the following rules;
1 2 X Y
3 4 Z T HERE if (X == D && Z == F) || (X == F && Z == D) sudoku cannot be D FK L solved AND if there is no common element, again it cannot be solved
M NO P
Here is my code. If there is a mistaken point, please leave a comment and let me know.
import java.util.ArrayList;
public class Q3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<String> seqs = new ArrayList<String>();
String bos = "";
String dolu = "1234";
findSubSeqs(seqs, bos, dolu);
ArrayList<String[]> sudokus = findAlternatives(seqs);
for (int a = 0;a < sudokus.size(); a++){
for (int b = 0; b < 4;b++){
System.out.println(sudokus.get(0)[b]);
}
System.out.println("--------------------");
}
System.out.println(sudokus.size());
}
public static void findSubSeqs(ArrayList<String> seqs, String bos, String dolu){
if (dolu.length()==0){
seqs.add(bos);
}
else{
for (int a = 0; a < dolu.length();a++){
String ch = dolu.charAt(a) + "";
bos += dolu.charAt(a) + "";
findSubSeqs(seqs, bos, dolu.replaceFirst(ch, ""));
bos = bos.substring(0, bos.length()-1);
}
}
}
public static ArrayList<String[]> findAlternatives(ArrayList<String> seqs){
ArrayList<String[]> arr = new ArrayList<String[]>();
for (int a = 0; a < seqs.size();a++){
String[] temp = new String[4];
temp[0] = seqs.get(a);
for (int b = 0; b < 2;b++){
if (b == 0){
temp[1] = seqs.get(a).charAt(2) + "";
temp[1] += seqs.get(a).charAt(3) + "";
}
else{
temp[1] = seqs.get(a).charAt(3) + "";
temp[1] += seqs.get(a).charAt(2) + "";
}
for (int c = 0; c < 2; c++){
temp[1] = temp[1].substring(0,2);
if (c == 0){
temp[1] += seqs.get(a).charAt(0) + "";
temp[1] += seqs.get(a).charAt(1) + "";
}
else{
temp[1] += seqs.get(a).charAt(1) + "";
temp[1] += seqs.get(a).charAt(0) + "";
}
for (int d = 0; d < 2; d++){
int f = Integer.parseInt(temp[0].substring(0, 1));
int s = Integer.parseInt(temp[1].substring(0,1));
int [] ds = new int[2];
int cs = 0;
for (int count = 1; count < 5; count++){
if (f != count && s != count){
ds[cs++] = count;
}
}
if (d == 0){
temp[2] = ds[0] + "";
}
else{
temp[2] = ds[1] + "";
}
for (int e = 0; e < 2; e++){
temp[2] = temp[2].substring(0,1);
if (e == 0){
temp[2] += f;
}
else
temp[2] += s;
if (((temp[2].charAt(0) + "").equals(temp[0].charAt(2) + "") && (temp[2].charAt(1)+"").equals(temp[1].charAt(2) + "")) || ((temp[2].charAt(0) + "").equals(temp[1].charAt(2) + "") && (temp[2].charAt(1) + "").equals(temp[0].charAt(2) + "")) ){
break;
}
if ((!(temp[2].charAt(0) + "").equals(temp[0].charAt(2) + "") && !(temp[2].charAt(1)+"").equals(temp[1].charAt(2) + "")) && (!(temp[2].charAt(0) + "").equals(temp[1].charAt(2) + "") && !(temp[2].charAt(1) + "").equals(temp[0].charAt(2) + ""))){
break;
}
for (int g = 1;g < 5; g++){
if (g != Integer.parseInt(temp[2].substring(0,1)) && g != Integer.parseInt(temp[2].substring(1,2)) && g != Integer.parseInt(temp[1].substring(2,3)) &&g != Integer.parseInt(temp[0].substring(2,3)) ){
temp[2] += g+ "";
}
}
for (int g = 1;g < 5; g++){
if (g != Integer.parseInt(temp[2].substring(0,1)) && g != Integer.parseInt(temp[2].substring(1,2)) && g != Integer.parseInt(temp[2].substring(2,3))){
temp[2] += g+ "";
}
}
temp[3] = "";
for (int g = 0;g <4;g++){
for (int h = 1;h < 5;h++){
if (h != Integer.parseInt(temp[0].substring(g,g+1)) && h != Integer.parseInt(temp[1].substring(g,g+1)) && h != Integer.parseInt(temp[2].substring(g,g+1))){
temp[3] += h+ "";
}
}
}
arr.add(temp);
}
}
}
}
}
return arr;
}
}
I think you have a mistake because you also consider the case that numbers can also be written in the form 03, 04, 05... I don't suppose that this is the case. To avoid this stuff you should add the statement
Integer.parseInt(s.substring(index, index + 1)) > 0
into your "if" which checks if temp is less than 26.
- nonameno January 16, 2015
EDIT: A = [0,1,2,3,4,9,5,7,6]
- nonameno October 29, 2015