Question #31529

We are given an array of integer and we have to equalise all the elements in minimum no. Of steps but there are constraint in following steps:
1. We can increase all the elements by 5 except the chosen one
2. We can increase all he elements by 2 except the chosen one
3. We can increase all the elements by 1 except the chosen one

Expert's answer

#include <iostream>

#include <conio.h>

using namespace std;

//main function

int main()

{

//array of integer

int arrayofinteger[5];

arrayofinteger[0]=5;

arrayofinteger[1]=10;

arrayofinteger[2]=15;

arrayofinteger[3]=2;

arrayofinteger[4]=3;

//find max number

int max=arrayofinteger[0];

for(int i=0;i<5;i++){

if(arrayofinteger[i]>max){

max=arrayofinteger[i];

}

}

for(int i=0;i<5;i++){

//1. We can increase all the elements by 5 except the chosen one

int diff=max-arrayofinteger[i];

if(diff==5){

arrayofinteger[i]+=5;

}

diff=max-arrayofinteger[i];

//2. We can increase all he elements by 2 except the chosen one

if(diff==2){

arrayofinteger[i]+=2;

}

diff=max-arrayofinteger[i];

//3. We can increase all the elements by 1 except the chosen one

if(diff==1){

arrayofinteger[i]+=1;

}else{

arrayofinteger[i]+=diff;

}

}

//show result

for(int i=0;i<5;i++){

cout<<arrayofinteger[i]<<"

";

}

int k;

cin>>k;

return 0;

}

#include <conio.h>

using namespace std;

//main function

int main()

{

//array of integer

int arrayofinteger[5];

arrayofinteger[0]=5;

arrayofinteger[1]=10;

arrayofinteger[2]=15;

arrayofinteger[3]=2;

arrayofinteger[4]=3;

//find max number

int max=arrayofinteger[0];

for(int i=0;i<5;i++){

if(arrayofinteger[i]>max){

max=arrayofinteger[i];

}

}

for(int i=0;i<5;i++){

//1. We can increase all the elements by 5 except the chosen one

int diff=max-arrayofinteger[i];

if(diff==5){

arrayofinteger[i]+=5;

}

diff=max-arrayofinteger[i];

//2. We can increase all he elements by 2 except the chosen one

if(diff==2){

arrayofinteger[i]+=2;

}

diff=max-arrayofinteger[i];

//3. We can increase all the elements by 1 except the chosen one

if(diff==1){

arrayofinteger[i]+=1;

}else{

arrayofinteger[i]+=diff;

}

}

//show result

for(int i=0;i<5;i++){

cout<<arrayofinteger[i]<<"

";

}

int k;

cin>>k;

return 0;

}

## Comments

## Leave a comment