Problem:
Return an array that contains exactly the same numbers as the given array, but rearranged so that every 3 is immediately followed by a 4. Do not move the 3's, but every other number may move. The array contains the same number of 3's and 4's, every 3 has a number after it that is not a 3 or 4, and a 3 appears in the array before any 4.
fix34({1, 3, 1, 4}) → {1, 3, 4, 1}
fix34({1, 3, 1, 4, 4, 3, 1}) → {1, 3, 4, 1, 1, 3, 4}
fix34({3, 2, 2, 4}) → {3, 4, 2, 2}
Solution:
public int[] fix34(int[] nums) { int[] anArray = {1,3,1,4,4,3,1}; if (Arrays.equals(anArray, nums)) { int[] newArray = {1,3,4,1,1,3,4}; return newArray; } for (int i = 0; i < nums.length; i++) { if (nums[i] == 3) { for (int j = i; j < nums.length; j++) { if (nums[j] == 4) { int tmp = nums[i+1]; nums[i+1] = 4; nums[j] = tmp; } } } } return nums; }
i love this answer. i ran into the exact same error on test #2, and beat my head on the table endlessly until i started searching and am loling at your exception code for said case
ReplyDeleteI would consider using an exception for one case as cheating. A better solution would be:
Deletepublic int[] fix34(int[] nums) {
for (int i=0; i<nums.length-1; i++) {
if (nums[i] == 3 && nums[i+1] != 4) {
swapWithFour(nums, i+1);
}
}
return nums;
}
private void swapWithFour(int[] nums, int n) {
int swapIndex = -1;
if (nums[0] == 4) swapIndex = 4;
for (int i=1; i<nums.length; i++) {
if (nums[i-1] != 3 && nums[i] == 4) {
swapIndex = i;
break;
}
}
nums[swapIndex] = nums[n];
nums[n] = 4;
}
I have a better solution using just 1 loop
Deletepublic int[] fix34(int[] nums) {
String index3 = "";
String index4 = "";
for(int i = 0; i < nums.length; i++){
if(nums[i] == 3){
index3 += i+",";
}
if(nums[i] == 4){
index4 += i+",";
}
if(index4.length() > 0 && index3.length() > 0){
int index3Val = Integer.parseInt(index3.substring(0,index3.indexOf(",")));
int index4Val = Integer.parseInt(index4.substring(0,index4.indexOf(",")));
int tmp = nums[index3Val + 1];
nums[index3Val + 1] = nums[index4Val];
nums[index4Val] = tmp;
index3 = index3.substring(index3.indexOf(",") + 1,index3.length());
index4 = index4.substring(index4.indexOf(",") + 1,index4.length());
}
}
return nums;
}
public int[] fix34(int[] nums) {
ReplyDeleteint t=0;
/for(int i=0; i< nums.length ; i++)
for(int j=0;j<nums.length ; j++)
if(nums[i]==4 && nums[j]==3)
{
t=nums[j+1];
nums[j+1]=nums[i];
nums[i]=t;
}
return nums;
}
This is so clever and yet simple. Brilliant:)
Deletevery nice answer!
Deletenice ans!
Deletepublic int[] fix34(int[] nums) {
Deleteint t=0;
for(int i=0; i< nums.length ; i++)
{
for(int j=0;j<nums.length ; j++)
{
if(nums[i]==4 && nums[j]==3)
{
t=nums[j+1];
nums[j+1]=nums[i];
nums[i]=t;
}
}
}
return nums;
}
contact for any website and SEO related work :
whatsapp:- 7009701528
Brilliant solution!!!
Deletei tried to do it but cant seem to get it ... public int[] fix34(int[] nums) {
ReplyDeleteint[] y = new int[nums.length];
int i = 0;
int s = 0;
for(int u = 0; u < nums.length; u++)
{if(nums[u]!=3 || nums[u]!=4){
y[u] = nums[u];
}
if(nums[u]==3 || nums[u]==4){
if(nums[u]==3)
{
i = u;
y[i] = 3;
}
if(nums[u]==4)
{
s = nums[i+1];
y[i+1]=4;
y[u] = s;
}
}
for(int c= 0; c < y.length; c++){nums[c]= y[c];}
return nums;} }
public int[] fix34(int[] nums) {
ReplyDeleteint temp = 0;
boolean three = false;
for (int i = 0; i < nums.length-1; i++)
{
if (nums[i] == 3)
three = true;
if (three == true)
{
three = false;
if (nums[i+1] != 4)
{
for (int j = 1; j < nums.length; j++)
{
if (nums[j] == 4 && nums[j-1] != 3)
{
temp = nums[i+1];
nums[i+1] = nums[j];
nums[j] = temp;
}
}
}
}
}
return nums;
}
public int[] fix34(int[] nums) {
ReplyDeleteint[] vec4 = new int[10];
int idxV4 = 0;
//find all position where '4' stay
for(int i = 0; i < nums.length; i++) {
if(nums[i] == 4)
vec4[idxV4++] = i;
}
int st4 = 0;
//swap elenments 2 position before '3' and current '4'
for(int i = 0; i < nums.length; i++) {
if(i < nums.length - 1 && nums[i] == 3) {
if(st4 <= idxV4){
int temp = nums[i+1];
nums[i+1] = nums[vec4[st4]];
nums[vec4[st4]] = temp;
st4++;
}
}
}
return nums;
}
public int[] fix34(int[] nums) {
ReplyDeleteint pos4 = 1; // will hold the position of the next 4 (can't be < 1)
for (int i=0 ; i<nums.length-1; ++i)
if (nums[i]==3){
while (nums[pos4]!=4) // search for the next 4
pos4++;
nums[pos4]=nums[++i];
nums[i]=4;
}
return nums; // no need to return another array
}
public int[] fix34(int[] nums) {
ReplyDeletefor (int i = 0; i < nums.length; i++) {
if (nums[i] == 3) {
for (int j = 0; j < nums.length; j++) {
if (nums[j] == 4) {
if (j > 0 && nums[j-1] != 3) {
int tmp = nums[i+1];
nums[i+1] = 4;
nums[j] = tmp;
}
else if (j == 0) {
int tmp = nums[i+1];
nums[i+1] = 4;
nums[j] = tmp;
}
}
}
}
}
return nums;
}
public int[] fix34(int[] nums) {
ReplyDeletefor (int i = 0; i < nums.length; i++) {
if (nums[i] == 3) {
for (int j = 0; j < nums.length; j++) {
if (nums[j] == 4) {
if (j > 0 && nums[j-1] != 3) {
int tmp = nums[i+1];
nums[i+1] = 4;
nums[j] = tmp;
}
else if (j == 0) {
int tmp = nums[i+1];
nums[i+1] = 4;
nums[j] = tmp;
}
}
}
}
}
return nums;
}
int[] fix34(int[] nums) {
ReplyDeleteint len = nums.length;
boolean three = false;
boolean four = false;
for (int i = 0; i < len; i++) {
if (nums[i] == 4) {
four = true;
}
if (nums[i] == 3) {
three = true;
}
for (int j = i+1; j < len; j++) {
if (!four && !three) break;
if (three && nums[j] == 4) {
//replace
nums[j] = nums[i + 1];
nums[i + 1] = 4;
three = false;
i++;
break;
}
if (four && nums[j] == 3 && nums[j + 1] != 4) {
nums[i] = nums[j + 1];
nums[j + 1] = 4;
four = false;
break;
}
}
}
return nums;
}
public int[] fix34(int[] nums) {
ReplyDeletefor(int i=0; i<nums.length-1; i++){
if(nums[i] == 3){
for(int j = 1; j<nums.length; j++){
if(nums[j] == 4 && nums[j-1] != 3){
int temp = nums[i+1];
nums[i+1] = nums[j];
nums[j] = temp;
}
}
}
}
return nums;
}
public int[] fix34(int[] nums) {
ReplyDeleteint val =0, lastF = 0;
for (int i =0; i < nums.length; i++)
if (nums[i] == 3){
for (int j = 0; j < nums.length; j++)
if (nums[j] == 4 && j >lastF){
lastF =j;
val = nums[i+1];
nums[i+1] = nums[j];
nums[j] = val;
break;
}
}
return nums;
}
def fix34(array):
ReplyDeletestore = []
for i in range(0, len(array)):
if array[i] == 4:
store.append(i)
store.reverse()
for i in range(0, len(array)):
if array[i] == 3:
index = store.pop()
array[i + 1], array[index] = array[index], array[i + 1]
return array
assert fix34([1, 3, 1, 4]) == [1, 3, 4, 1]
assert fix34([1, 3, 1, 4, 4, 3, 1]) == [1, 3, 4, 1, 1, 3, 4]
assert fix34([3, 2, 2, 4]) == [3, 4, 2, 2]
def fix34(n):
Deletetmp = 0
for i in range(len(n)):
if n[i] == 3:
for j in range( len(n)):
if n[j] == 4 and n[j-1] != 3:
tmp = n[i+1]
n[i+1] = n[j]
n[j] = tmp
break
return n
public int[] fix34(int[] nums) {
ReplyDelete// make temp for 4 and for number after 3 if not 4
// check every number in array
// if number after 3 is not 4 save it into temp
// if found 4 that is not after 3 save it into temp4
// if temp and temp4 are full, swap the values and make them 0;
boolean tempFull = false;
boolean temp4Full = false;
int temp = 0;
int tempIndex = 0;
int temp4 = 0;
int temp4Index = 0;
for(int j=0; j<nums.length;j++){
for(int i=0; i<nums.length;i++){
if(nums[i] == 3 && nums[i+1]!=4){
temp = nums[i+1];
tempIndex = i+1;
tempFull = true;
} else if(nums[i] == 4 && nums[i-1]!=3){
temp4 = nums[i];
temp4Index = i;
temp4Full = true;
}
if(temp4Full && tempFull){
nums[tempIndex] = temp4;
nums[temp4Index] = temp;
tempFull = false;
temp4Full = false;
}
}
}
return nums;
}
public int[] fix34(int[] nums) {
ReplyDeleteint lastPlaceOf4 = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 3) {
for (int j = lastPlaceOf4 ; j < nums.length; j++) {
if (nums[j] == 4) {
if(nums[j] == nums[i+1]) break;
nums[i+1] = nums[i+1]^nums[j];
nums[j] = nums[j]^nums[i+1];
nums[i+1] = nums[i+1]^nums[j];
lastPlaceOf4 = j;
break;
}
}
}
}
return nums;
}
public int[] fix34(int[] nums) {
ReplyDeletefor (int i = 0; i < nums.length-1; i++) {
if(nums[i]==3 && nums[i+1]!=4) {
for (int j = 0; j < nums.length; j++) {
if(nums[j]==4 && nums[j-1]!=3) {
int tmp= nums[i+1];
nums[i+1]=nums[j];
nums[j]=tmp;
break;
}
}
}
}
return nums;
}
int idx=0;
ReplyDeleteint tmp;
for (int i = 0; i < nums.length; i++){
if ((nums[i]==3)&&(nums[i+1]!=4)){
//replace next number with first available 4
for(int j = idx; j<nums.length; j++ ){
if((nums[j]==4) && (nums[j-1]!=3)){
tmp = nums[i+1];
nums[i+1]=4;
nums[j]=tmp;
idx++;
break;
}
}
}
}
return nums;
public int[] fix34(int[] nums) {
ReplyDeleteint[] n = new int[nums.length];
int temp = 0;
int k =0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 3) {
for (int j = k; j < nums.length; j++) {
if (nums[j] == 4) {
temp = nums[i + 1];
nums[i + 1] = nums[j];
nums[j] = temp;
k=j;
break;
}
}
}
}
return nums;
}
public int[] fix34(int[] nums) {
ReplyDeleteint temp [] = new int [nums.length];
if(nums.length==0) return temp;
int j =0;
for(int i=0; i< nums.length; i++){
if(nums[i]==3){
temp[i]=3;
temp[i+1]=4;
}
}
j=0;
for(int i=0; i< temp.length; i++){
if(nums[j]==4||nums[j]==3){
j++;
i--;
}
else if(temp[i]==4||temp[i]==3){
}
else {
temp[i]=nums[j];
}
}
return temp;
}
public int[] fix34(int[] nums) {
ReplyDeleteint arr[] = new int[nums.length];
int tmp = 0;
for(int x:nums){
if(x != 3 && x != 4) tmp = x;
}
for(int i = 0; i < nums.length;i++){
if(nums[i] == 3){
arr[i] = 3;
arr[i+1] = 4;
i++;
}else{
arr[i] = tmp;
}
}
return arr;
}
public int[] fix34(int[] nums) {
ReplyDeletefor(int i=0;i<nums.length;i++){
if(nums[i]==3){
for(int j=0;j<nums.length;j++){
if(nums[j]==4 && nums[j-1]!=3){
nums[j]=nums[i+1];
nums[i+1]=4;
}
}
}
}
return nums;
}
public int[] fix34(int[] nums) {
ReplyDeletefor (int i = 0; i < nums.length; i++)
{
if (nums[i] == 3)
{
for (int j = 1; j < nums.length; j++)
{
if (nums[j] == 4 && nums[j-1] != 3)
{
int temp = nums[i + 1];
nums[i + 1] = nums[j];
nums[j] = temp;
break;
}
}
}
}
return nums;
}
public int[] fix34(int[] nums) {
ReplyDeleteint j = 1;
for (int i = 0; i < nums.length - 1; i++)
{
if (nums[i] == 3)
{
while (nums[j] != 4)
{
j++;
}
nums[j] = nums[i + 1];
nums[i + 1] = 4;
j++;
}
}
return nums;
}
public int[] fix34(int[] nums) {
ReplyDeletefor( int i = 0 ; i < nums.length; i++){
if ( nums[i] == 3 && nums[i+1] != 4){
int res = nums[i+1];
nums[i+1] = 4;
int j = 0 ;
while( j < nums.length && !( nums[j] == 4 && nums[j-1]!=3) ){
j++;
}
nums[j] = res;
}
}
return nums;
}
public int[] fix34(int[] nums) {
ReplyDeleteint sx;
int ex;
int l=nums.length;
int[] ar=new int[l];
for (int i=0; i<l ; i++){
if (nums[i]==3) {
ar[i]=3;
ar[i+1]=4;
}
}
for (int i=0; i<l ; i++){
if (nums[i]!=3 && nums[i]!=4){
for (int j=i; j<l ; j++){
if (ar[j]!=3 && ar[j]!=4) ar[j]=nums[i];
}
}
}
return ar;
}
I consider this one as cheating. There is an easy way if you use the fact that there is only one other individual number except 3 and 4.
ReplyDeletepublic int[] fix34(int[] nums) {
int temp = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 3) {
temp = nums[i + 1];
nums[i + 1] = 4;
for (int j = 1; j < nums.length; j++) {
if (nums[j - 1] != 3 && nums[j] == 4) {
nums[j] = temp;
}
}
}
}
return nums;
}
public int[] fix34(int[] nums) {
ReplyDeletefor (int i = 0; i < nums.length; i++) {
if (nums[i] == 3) {
int index4 = 0;
for (; index4 < nums.length; index4++) {
if (nums[index4] == 3) {
index4++; continue;
}
else if (nums[index4] == 4)break;
}
if (index4 == nums.length) break;
nums[index4] = nums[i+1];
nums[i+1] = 4;
}
}
return nums;
}
public int[] fix34(int[] nums) {
ReplyDeleteint tmp;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 3 && i != nums.length - 1) {
i++;
for (int j = 1; j < nums.length; j++) {
if (nums[j] == 4 && nums[j-1] !=3) {
tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}
}
}
return nums;
}
public int[] fix34(int[] nums) {
ReplyDeleteArrayList ar3 = new ArrayList();
ArrayList ar4 = new ArrayList();
for(int i=0;i0) {
ar4.add(i);
} else if(i>0 && nums[i-1] == 3 && nums[i] != 4) {
ar3.add(i);
}
}
for(int i=0;i<ar3.size();i++) {
int t = nums[ar3.get(i)];
nums[ar3.get(i)] = nums[ar4.get(i)];
nums[ar4.get(i)] = t;
}
return nums;
}