Amazon Interview Question
SDE1sCountry: United States
Interview Type: Written Test
Yet another approach is to form pairs of numbers from 4!(=24) permutations. Pairing should happen in such a way that at a given position, the maximum digit of one number should match with the minimum digit of the other number, the second maximum digit should match with the second minimum and so on.. For ex:
Pair up 1234 with 4321, 2134 with 3421 etc. The sum of each of these pairs is equal to 5555 and we have 12 pairs out of the total 24 permutations.
5555 * 12 = 66660
Here we don't have to do actual calculation, else we will never get answer in written test. Think of this as mathematical + logical question. Lets say we have all permutation of 1234. It will be 4! = 24. Now if we look at each column of this list carefully, every number will appear for 6 time.
1234
1324
2134
2314
3124
3214
...
Now if you just concentrate on 4th column, "4" is coming 6 times, same way if we list all number each number will come 6 time in 4th column. So sum of 4th column is (4*6 + 3*6 + 2*6 + 1*6) = 60.
Same way, for all other columns sum would be 60.
Now we are just one step far away from answer. And that is consider "carry". 4th column will give 6 as carry to 3rd columns, again in turn 3rd column will give 6 as carry to 2nd column and so on.
Therefore, final answer would be 66660.
Here is the java program which will print the combinations and sum of all combinations. Little more info apart from sum.
package com.bala.logical;
import java.util.ArrayList;
import java.util.List;
public class SumCombination {
static String finalString = "";
public static void main(String[] args) {
String s = "1234";
List<String> numList = new ArrayList<String>();
System.out.println(arrangeNumbers(s, numList));
System.out.println("Size of the elements "+numList.size());
int sum = 0;
for(String combination: numList){
sum = sum + Integer.parseInt(combination);
}
System.out.println(sum);
}
public static String[] swap(String s) {
String[] combinations = new String[s.length()];
if (s.length() == 1) {
combinations[0] = s;
return combinations;
}
for (int i = 0; i < s.length(); i++) {
combinations[i] = s.substring(1, s.length()) + s.substring(0, 1);
s = s.substring(1, s.length()) + s.substring(0, 1);
}
return combinations;
}
public static List<String> arrangeNumbers(String s, List<String> numberList) {
String[] combinations = swap(s);
for (int i = 0; i < combinations.length; i++) {
finalString = finalString + combinations[i].substring(0, 1);
if (combinations.length == 1) {
numberList.add(finalString);
} else {
String subS = combinations[i].substring(1,
combinations[i].length());
arrangeNumbers(subS, numberList);
}
finalString = finalString.substring(0, finalString.length() - 1);
}
return numberList;
}
}
it can be done by finding all permutation of the given digits without rep, then add each numbers formed. there are totally 4! numbers to add.
Let number = 1234
let sum = 1234.
1. Find the largest index k such that a[k] < a[k + 1]. If no such index exists, the permutation is the last permutation.
2. Find the largest index l such that a[k] < a[l]. Since k + 1 is such an index, l is well defined and satisfies k < l.
3. Swap a[k] with a[l].
4. Reverse the sequence from a[k + 1] up to and including the final element a[n].
5. sum = sum + newly formed number on step 4.
Here is the java program which will print the combinations and sum of all combinations. Little more info apart from sum.
package com.bala.logical;
import java.util.ArrayList;
import java.util.List;
public class SumCombination {
static String finalString = "";
public static void main(String[] args) {
String s = "1234";
List<String> numList = new ArrayList<String>();
System.out.println(arrangeNumbers(s, numList));
System.out.println("Size of the elements "+numList.size());
int sum = 0;
for(String combination: numList){
sum = sum + Integer.parseInt(combination);
}
System.out.println(sum);
}
public static String[] swap(String s) {
String[] combinations = new String[s.length()];
if (s.length() == 1) {
combinations[0] = s;
return combinations;
}
for (int i = 0; i < s.length(); i++) {
combinations[i] = s.substring(1, s.length()) + s.substring(0, 1);
s = s.substring(1, s.length()) + s.substring(0, 1);
}
return combinations;
}
public static List<String> arrangeNumbers(String s, List<String> numberList) {
String[] combinations = swap(s);
for (int i = 0; i < combinations.length; i++) {
finalString = finalString + combinations[i].substring(0, 1);
if (combinations.length == 1) {
numberList.add(finalString);
} else {
String subS = combinations[i].substring(1,
combinations[i].length());
arrangeNumbers(subS, numberList);
}
finalString = finalString.substring(0, finalString.length() - 1);
}
return numberList;
}
}
How about this approach?
- Murali Mohan June 26, 2013With 4 placed in units place, there are 3! permutations of 123 in thousandths, hundredths and tenths place. That means 4 occurs in units place 3! times.
Similarly 3 2 and 1 each occur in units place 3! times. The sum of all numbers in units place is therefore 3!(4 + 3 + 2 + 1) = 6 * 10 = 60.
The same value happens to occur in tenths, hundredths and thousandths place as well, except that it gets multiplied by a power of 10 according to its position.
So, the total should be 60 + 600 + 6000 + 60000 = 66660.