Don’t worry, unlock all articles / blogs on PrepInsta by just simply logging in on our website
HackerRank Coding Question for Placements 2
June 24, 2019
We are given an array with n elements from {1,2,3,4}. Find the number of minimum changes required to be performed so that no two adjacent numbers are same?
Please write the code in the comments? We will add them here.
For an array with elements 0 & 1 only, program to find required number of steps so that no adjacent elements are same –
#include
#include
using namespace std;
bool chk_bin(int arr[], int size){
bool flag = true;
for(int i=0;i<size;i++){
if(arr[i]!=1 && arr[i]!=0){
cout<<"not binary"<<endl;
flag = false;
}
}
return flag;
}
int conversions(int arr[], int size){
int c = 0;
for(int i=0;i<size;i++){
/*for(int j=i+1;j<size;j++){
if(arr[i]==arr[j]){
replace(&arr[j]);
c++;
}
}*/
if(arr[i]==arr[i+1]){
replace(&arr[i+1]);
c++;
}
}
return c;
}
int main(){
// keep this function call here
int A[] = {1, 0, 0, 1, 0, 0, 1, 0};
import random
n=int(input())
c=0
l=[]
for i in range(n):
l.append(random.randint(1,4))
print(l)
for i in range(len(l)-1):
if l[i]!=l[i+1]:
c+=1
print(c)
Here is the solution :-
#include
using namespace std;
int main() {
int arr[100];
int n,c=0;
cin>>n;
for(int i=0;i>arr[i];
}
for(int i=0;i<n;i++)
{
if(arr[i] == arr[i+1])
{
continue;
}
else{
c++;
}
}
cout<<c-1;
import random
n=int(input(“Enter number “))
a=[1,2,3,4]
arr=[]
for i in range(n):
x=random.choice(a)
arr.append(x)
print(arr)
y=[]
check=0
for i in range(1,n-1):
a=[1,2,3,4]
if arr[i-1]==arr[i] or arr[i+1]==arr[i]:
check+=1
a.remove(arr[i-1])
a.remove(arr[i])
y=a
c=random.choice(y)
arr[i]=c
#python_solution for this question
n = int(input())
a = []
c = 0
for i in range(n):
b = int(input())
a.append(b)
print(a)
for i in range(n-1):
if a[i] == a[i+1]:
c = c+1
print(“The number of time they have to be changed “, c)
For an array with elements 0 & 1 only, program to find required number of steps so that no adjacent elements are same –
#include
#include
using namespace std;
//ALL RIGHT
void replace(int *digit){
if(*digit==1){
*digit=0;
}
else{
*digit=1;
}
}
bool chk_bin(int arr[], int size){
bool flag = true;
for(int i=0;i<size;i++){
if(arr[i]!=1 && arr[i]!=0){
cout<<"not binary"<<endl;
flag = false;
}
}
return flag;
}
int conversions(int arr[], int size){
int c = 0;
for(int i=0;i<size;i++){
/*for(int j=i+1;j<size;j++){
if(arr[i]==arr[j]){
replace(&arr[j]);
c++;
}
}*/
if(arr[i]==arr[i+1]){
replace(&arr[i+1]);
c++;
}
}
return c;
}
int main(){
// keep this function call here
int A[] = {1, 0, 0, 1, 0, 0, 1, 0};
int arrLength = sizeof(A) / sizeof(*A);
/*for(int i=0;i<arrLength;i++){
replace(&A[i]);
cout<<A[i]<<" ";
}
cout<<endl;*/
if(chk_bin(A, arrLength)){
cout<<"Binary "<<endl;
cout<<conversions(A, arrLength)<<endl;
}
else{
cout<<"Not binary";
};
//cout<<"Size of array : "<<arrLength<<endl;
return 0;
}
//java Lanaguage
public int minumn_chances(int a[])
{
int counter=0;
for(int j=0;j<a.length-1;i++)
{
if(a[i]!=a[i+1])
counter+=1;
}
return counter;
}
import random
n=int(input())
c=0
l=[]
for i in range(n):
l.append(random.randint(1,4))
print(l)
for i in range(len(l)-1):
if l[i]!=l[i+1]:
c+=1
print(c)
#include
using namespace std;
int main(){
int arr[100];
int n,i;
cout<>n;
cout<<"enter array values";
for( i=0;i> arr[i];
}
int c=0;
for(i=0;i<n-1;i++){
if(arr[i]==arr[i+1]){
c++;
i++;
}
}
cout<<c;
return 0;
}
Here is the solution :-
#include
using namespace std;
int main() {
int arr[100];
int n,c=0;
cin>>n;
for(int i=0;i>arr[i];
}
for(int i=0;i<n;i++)
{
if(arr[i] == arr[i+1])
{
continue;
}
else{
c++;
}
}
cout<<c-1;
}
can you explain me
import random
n=int(input(“Enter number “))
a=[1,2,3,4]
arr=[]
for i in range(n):
x=random.choice(a)
arr.append(x)
print(arr)
y=[]
check=0
for i in range(1,n-1):
a=[1,2,3,4]
if arr[i-1]==arr[i] or arr[i+1]==arr[i]:
check+=1
a.remove(arr[i-1])
a.remove(arr[i])
y=a
c=random.choice(y)
arr[i]=c
print(arr)
print(check)
#include
#include
# define MAX_SIZE 100
int main()
{
int a[MAX_SIZE];
int s,i,j,k;
printf(“enter the size of an array”);
scanf(“%d”,&s);
for(i=0;i<s;i++)
{
scanf("%d",&a[i]);
}
printf("the elements are\n");
for(i=0;i<s;i++)
{
printf("%d\n",a[i]);
}
for(i=0;i<s;i++)
for(j=i+1;j<s;j++)
{for(k=j+1;k<s;k++)
if(a[i]==a[j]){
int temp=a[i+1];
a[i+1]=a[k];
a[k]=temp;
}
}
for(i=0;i<s;i++){
printf("%d\n",a[i]);}
}
the number of changes required is n, i.e. segregate the array into even and odd numbers. To do that we have to parse array once.
#include
using namespace std;
int main()
{
int n,count=0;
cin>>n;
int a[n];
for(int i=0;i>a[i];
int i=0;
while(i<n-1)
{
if(a[i]==a[i+1]){
count++;
i=i+2;
}
else
i++;
}
cout<<count;
}
Please write here