#include <bits/stdc++.h>
using namespace std;
int main()
{
// index 0 1 2
int b[3] = {5,2,1};
int n=3;
int count=0;
// -------------- boolean logic we fix here -------------------------------
while(true) // intially it will be true
{
bool allEqual = true; // Intially allEqual we set as true becoz when we get all number equal then bool should give true
for(int i=0; i<n; i++)
{
if(b[i] != b[0]) // b[i] current element is not equal b[0] means the starting element
{
allEqual = false; // false condition
break;
}
}
if(allEqual) // sagale element Equal rahile
{
break; // break karnar means i got my answer
}
//----------------------------------------------------------------------
int largest = INT_MIN;
int secondlargest = INT_MIN;
int maxIndex = -1; // when nothing I got Equal then print -1
//------------ first largest number logic ----------------------
// Iteration 1:
// 5 > largest
// largest = 5
// maxIndex = 0
for(int i=0; i<n; i++)
{
if(b[i] > largest)
{
largest = b[i];
maxIndex = i;
}
}
// ---------------------------secondlargest number logic and to get update the first largest number with secondlargest number------------
//Iteration 2:
// if( 2 != 5 && 5 > 2)
// secondlargest = 2
// b[0] = 2 // mhnje b[0] madhe 5 number hota na ata to replace houn 2 number store hoil update honar tar mag count increment karyache
//count increment : 1
// array becomes : 2 2 1
//Iteration 3:
// if( 1 != 5 && 1 > 2)
//secondlargest = 1
//b[0] = 1 mhnje b[0] madhe 5 number hota na ata to replace houn 1 number store hoil update honar tar mag count increment karyache
//count increment : 1
// array becomes : 1 2 1
// iteration ends
//At last final all values will becomes 1 1 1
for(int i=0; i<n; i++)
{
if(b[i] != largest && b[i] > secondlargest) // b[i] not equal to largest
{
secondlargest = b[i];
}
}
b[maxIndex] = secondlargest; // mhnje secondlargest element la b[maxIndex] madhe store krt jaycha mhnje largestelement la secondlargest replace hott rahil
count = count + 1; // counter increment karat jayacha
cout<<"count: "<<count;
cout<<"final Array(Equal Values)";
for(int i=0; i<n; i++)
{
cout<<b[i]<<" ";
}
cout<<endl;
}
}
/*
Output:
count: 1final Array(Equal Values)2 2 1
count: 2final Array(Equal Values)1 2 1
count: 3final Array(Equal Values)1 1 1
*/