Samsung Interview Question
Developer Program EngineersCountry: India
Interview Type: Written Test
If my understanding is correct, following is the way to solve this.
Taking a simple example of 2 numbers spread over 3 place.
array A = {1,2}; n=3
say the number is: xyz
I see that repetition is allowed, as per the example.
when n=3, we have to take into account n=2 and n=1 based on what I see from the example given in the question.
x can have 1 or 2; y also can have 1 or 2; z also can have 1 or 2.
So the possibilities of all types of 3 digit numbers is: 2*2*2 = 8
Same way, possibilities of all types of 2 digit numbers is 2*2=4
Same way possibilities of all types of 1 digit number is 2
In this case answer would be 14.
Going by this, following snippet would reap the result.
numOfPlaces = 1
while(numOfPlaces<=n) {
result = result + array.length ^ numOfPlaces;
numOfPlaces++;
}
Note: I have not tested the while loop. but conceptually, I believe I have given a fair answer based on the question.
Correct me if wrong.
import java.util.ArrayList;
import java.util.List;
public class IntegerArrayGroups {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
int n = 5;
List<String> result = generateGroups(arr, n);
System.out.println("Groups for n = " + n + ":");
for (int i = 0; i < result.size(); i++) {
System.out.println((i + 1) + ". " + result.get(i));
}
System.out.println("Total groups: " + result.size());
}
public static List<String> generateGroups(int[] arr, int n) {
List<String> groups = new ArrayList<>();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
generateGroupsUtil(arr, 0, i, sb, groups);
}
return groups;
}
private static void generateGroupsUtil(int[] arr, int index, int length, StringBuilder sb, List<String> groups) {
if (length == 0) {
groups.add(sb.toString());
return;
}
for (int i = index; i < arr.length; i++) {
sb.append(arr[i]);
generateGroupsUtil(arr, i, length - 1, sb, groups);
sb.deleteCharAt(sb.length() - 1);
}
}
}
Now, when you run this code with n = 5 and the array {1, 2, 3, 4}, it will produce the output with the count of arrays along with the arrays themselves. Similarly, you can change the value of n and the array arr to generate groups for different scenarios.
import java.util.ArrayList;
import java.util.List;
public class IntegerArrayGroups {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
int n = 5;
List<String> result = generateGroups(arr, n);
System.out.println("Groups for n = " + n + ":");
for (int i = 0; i < result.size(); i++) {
System.out.println((i + 1) + ". " + result.get(i));
}
System.out.println("Total groups: " + result.size());
}
public static List<String> generateGroups(int[] arr, int n) {
List<String> groups = new ArrayList<>();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
generateGroupsUtil(arr, 0, i, sb, groups);
}
return groups;
}
private static void generateGroupsUtil(int[] arr, int index, int length, StringBuilder sb, List<String> groups) {
if (length == 0) {
groups.add(sb.toString());
return;
}
for (int i = index; i < arr.length; i++) {
sb.append(arr[i]);
generateGroupsUtil(arr, i, length - 1, sb, groups);
sb.deleteCharAt(sb.length() - 1);
}
}
}
Now, when you run this code with n = 5 and the array {1, 2, 3, 4}, it will produce the output with the count of arrays along with the arrays themselves. Similarly, you can change the value of n and the array arr to generate groups for different scenarios.
import java.util.ArrayList;
import java.util.List;
public class IntegerArrayGroups {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
int n = 5;
List<String> result = generateGroups(arr, n);
System.out.println("Groups for n = " + n + ":");
for (int i = 0; i < result.size(); i++) {
System.out.println((i + 1) + ". " + result.get(i));
}
System.out.println("Total groups: " + result.size());
}
public static List<String> generateGroups(int[] arr, int n) {
List<String> groups = new ArrayList<>();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
generateGroupsUtil(arr, 0, i, sb, groups);
}
return groups;
}
private static void generateGroupsUtil(int[] arr, int index, int length, StringBuilder sb, List<String> groups) {
if (length == 0) {
groups.add(sb.toString());
return;
}
for (int i = index; i < arr.length; i++) {
sb.append(arr[i]);
generateGroupsUtil(arr, i, length - 1, sb, groups);
sb.deleteCharAt(sb.length() - 1);
}
}
}
Now, when you run this code with `n = 5` and the array `{1, 2, 3, 4}`, it will produce the output with the count of arrays along with the arrays themselves. Similarly, you can change the value of `n` and the array `arr` to generate groups for different scenarios.
import java.util.ArrayList;
import java.util.List;
public class IntegerArrayGroups {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
int n = 5;
List<String> result = generateGroups(arr, n);
System.out.println("Groups for n = " + n + ":");
for (int i = 0; i < result.size(); i++) {
System.out.println((i + 1) + ". " + result.get(i));
}
System.out.println("Total groups: " + result.size());
}
public static List<String> generateGroups(int[] arr, int n) {
List<String> groups = new ArrayList<>();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
generateGroupsUtil(arr, 0, i, sb, groups);
}
return groups;
}
private static void generateGroupsUtil(int[] arr, int index, int length, StringBuilder sb, List<String> groups) {
if (length == 0) {
groups.add(sb.toString());
return;
}
for (int i = index; i < arr.length; i++) {
sb.append(arr[i]);
generateGroupsUtil(arr, i, length - 1, sb, groups);
sb.deleteCharAt(sb.length() - 1);
}
}
}
import java.util.ArrayList;
import java.util.List;
public class IntegerArrayGroups {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
int n = 5;
List<String> result = generateGroups(arr, n);
System.out.println("Groups for n = " + n + ":");
for (int i = 0; i < result.size(); i++) {
System.out.println((i + 1) + ". " + result.get(i));
}
System.out.println("Total groups: " + result.size());
}
public static List<String> generateGroups(int[] arr, int n) {
List<String> groups = new ArrayList<>();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
generateGroupsUtil(arr, 0, i, sb, groups);
}
return groups;
}
private static void generateGroupsUtil(int[] arr, int index, int length, StringBuilder sb, List<String> groups) {
if (length == 0) {
groups.add(sb.toString());
return;
}
for (int i = index; i < arr.length; i++) {
sb.append(arr[i]);
generateGroupsUtil(arr, i, length - 1, sb, groups);
sb.deleteCharAt(sb.length() - 1);
}
}
}
Question is not clear. Can you please rephrase the question and edit the question. When n > 0, doesn't it imply that it can be greater than 4?? One more doubt I have is given example for n=5
- prudent_programmer March 05, 20181,11,111,1111,11111,12341 , the numbers 222, 2222, 333, 3333, .... are also valid right?