Thirumaleshwar Kunamalla
BAN USER- 1of 1 vote
AnswersIn Amazon web site, the product items has to show with different attributes combination for clothers.
Example:
color - red blue green
size - XL X M S
pattern - checks lines
so output should be in below format in different combinations:
red - xL - checks
red - xL - lines
red - X - checks
red - x - lines
red - M - checks
:
:
green - S - checks
green - S - lines
Note:- In above example, no. of attributes is 3. but attributes can be N.
Below is the code, I have written. Hope it will be useful for anyone.
This is an non-recursive logic which will work for large value of N. time Complexity is O(n2).------------------------------------------------------------------------------------- package com.test; import java.util.Scanner; public class Solution { public static void main(String args[] ) throws Exception { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ Scanner in = new Scanner(System.in); int n = in.nextInt(); in.nextLine(); //to read new line String[][] attributes = new String[n][]; int i=0; while(i < n) { String temp = in.nextLine(); String[] values = temp.split(" "); attributes[i] = values; i++; } printAttributesCombination(attributes); } static void printAttributesCombination(String[][] attributes) { int count[] = new int[attributes.length]; int totalcount[] = new int[attributes.length]; //initialize the totalcount and temp index count initialize(count, totalcount, attributes); while(isDone(count,totalcount)) { printArray(attributes,count); } } static void initialize(int count[], int totalcount[], String[][] attributes) { for(int i = 0; i < count.length; i++) { count[i] = 0; totalcount[i] = attributes[i].length - 1; } count[count.length-1] = -1; } static boolean isDone(int count[], int totalCount[]) { boolean prevIndexSet = true; boolean canTerminateLoop = false; int i = 0; for(i = count.length - 1; i >= 0 ; i--) { if(count[i] == totalCount[i]) { count[i] = 0; prevIndexSet = true; canTerminateLoop = true; } else { count[i] = count[i] + 1; prevIndexSet = false; canTerminateLoop = false; } if(!prevIndexSet) break; } if(canTerminateLoop && i == -1 ) return false; return true; } static void printArray(String[][] arr, int count[]) { System.out.println(); for(int i = 0; i < arr.length; i++) { System.out.print(" " + arr[i][count[i]]); } } } -------------------------------------------------------------------------------------
Output:
- Thirumaleshwar Kunamalla in United States
==============
Sample1:-
============
3
a b c
d e f
g h i
a d g
a d h
a d i
a e g
a e h
a e i
a f g
a f h
a f i
b d g
b d h
b d i
b e g
b e h
b e i
b f g
b f h
b f i
c d g
c d h
c d i
c e g
c e h
c e i
c f g
c f h
c f i
Sample2:-
============
4
a b
a b c
d e
f i
a a d f
a a d i
a a e f
a a e i
a b d f
a b d i
a b e f
a b e i
a c d f
a c d i
a c e f
a c e i
b a d f
b a d i
b a e f
b a e i
b b d f
b b d i
b b e f
b b e i
b c d f
b c d i
b c e f
b c e i| Report Duplicate | Flag | PURGE
Amazon SDE-2
Below is the code, I have written , not able to do within time in written test. Hope it will be useful for anyone.
This is an non-recursive logic which will work for large value of N. time Complexity is O(n2).
-------------------------------------------------------------------------------------
package com.test;
import java.util.Scanner;
public class Solution
{
public static void main(String args[] ) throws Exception
{
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine(); //to read new line
String[][] attributes = new String[n][];
int i=0;
while(i < n)
{
String temp = in.nextLine();
String[] values = temp.split(" ");
attributes[i] = values;
i++;
}
printAttributesCombination(attributes);
}
static void printAttributesCombination(String[][] attributes)
{
int count[] = new int[attributes.length];
int totalcount[] = new int[attributes.length];
//initialize the totalcount and temp index count
initialize(count, totalcount, attributes);
while(isDone(count,totalcount))
{
printArray(attributes,count);
}
}
static void initialize(int count[], int totalcount[], String[][] attributes)
{
for(int i = 0; i < count.length; i++)
{
count[i] = 0;
totalcount[i] = attributes[i].length - 1;
}
count[count.length-1] = -1;
}
static boolean isDone(int count[], int totalCount[])
{
boolean prevIndexSet = true;
boolean canTerminateLoop = false;
int i = 0;
for(i = count.length - 1; i >= 0 ; i--)
{
if(count[i] == totalCount[i])
{
count[i] = 0;
prevIndexSet = true;
canTerminateLoop = true;
}
else
{
count[i] = count[i] + 1;
prevIndexSet = false;
canTerminateLoop = false;
}
if(!prevIndexSet)
break;
}
if(canTerminateLoop && i == -1 )
return false;
return true;
}
static void printArray(String[][] arr, int count[])
{
System.out.println();
for(int i = 0; i < arr.length; i++)
{
System.out.print(" " + arr[i][count[i]]);
}
}
}
-------------------------------------------------------------------------------------
Output:
==============
Sample1:-
============
3
a b c
d e f
g h i
a d g
a d h
a d i
a e g
a e h
a e i
a f g
a f h
a f i
b d g
b d h
b d i
b e g
b e h
b e i
b f g
b f h
b f i
c d g
c d h
c d i
c e g
c e h
c e i
c f g
c f h
c f i
Sample2:-
============
4
a b
a b c
d e
f i
a a d f
a a d i
a a e f
a a e i
a b d f
a b d i
a b e f
a b e i
a c d f
a c d i
a c e f
a c e i
b a d f
b a d i
b a e f
b a e i
b b d f
b b d i
b b e f
b b e i
b c d f
b c d i
b c e f
b c e i
Hope this code will help you. It will not used any additional packages.
-----------------------------------------------------
}
- Thirumaleshwar Kunamalla July 04, 2014