TC: O(3!) = constant
class Solution {
int max = 0;
public int maxGoodNumber(int[] nums) {
int visited[] = new int[3];
List<Integer> list = new ArrayList<>();
find(nums,visited,list);
return max;
}
public void find(int nums[], int visited[],List<Integer> list){
//base case
if(list.size()==3){
//System.out.println(list);
max = Math.max(max,Integer.parseInt(Integer.toBinaryString(list.get(0)) + Integer.toBinaryString(list.get(1)) +
Integer.toBinaryString(list.get(2)),2));
return;
}
for(int i = 0;i<3;i++){
if(visited[i]!=1){
visited[i] =1;
list.add(nums[i]);
find(nums,visited,list);
visited[i] = 0;
list.remove(list.size()-1);
}
}
}
}
Top comments (0)