fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. // index 0 1 2
  7. int b[3] = {5,2,1};
  8. int n=3;
  9. int count=0;
  10.  
  11. // -------------- boolean logic we fix here -------------------------------
  12. while(true) // intially it will be true
  13. {
  14. bool allEqual = true; // Intially allEqual we set as true becoz when we get all number equal then bool should give true
  15. for(int i=0; i<n; i++)
  16. {
  17. if(b[i] != b[0]) // b[i] current element is not equal b[0] means the starting element
  18. {
  19. allEqual = false; // false condition
  20. break;
  21. }
  22. }
  23. if(allEqual) // sagale element Equal rahile
  24. {
  25. break; // break karnar means i got my answer
  26. }
  27. //----------------------------------------------------------------------
  28.  
  29. int largest = INT_MIN;
  30. int secondlargest = INT_MIN;
  31. int maxIndex = -1; // when nothing I got Equal then print -1
  32.  
  33. //------------ first largest number logic ----------------------
  34. // Iteration 1:
  35. // 5 > largest
  36. // largest = 5
  37. // maxIndex = 0
  38. for(int i=0; i<n; i++)
  39. {
  40.  
  41. if(b[i] > largest)
  42. {
  43. largest = b[i];
  44. maxIndex = i;
  45. }
  46. }
  47.  
  48. // ---------------------------secondlargest number logic and to get update the first largest number with secondlargest number------------
  49. //Iteration 2:
  50. // if( 2 != 5 && 5 > 2)
  51. // secondlargest = 2
  52. // 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
  53. //count increment : 1
  54. // array becomes : 2 2 1
  55.  
  56. //Iteration 3:
  57. // if( 1 != 5 && 1 > 2)
  58. //secondlargest = 1
  59. //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
  60. //count increment : 1
  61. // array becomes : 1 2 1
  62.  
  63. // iteration ends
  64.  
  65. //At last final all values will becomes 1 1 1
  66. for(int i=0; i<n; i++)
  67. {
  68. if(b[i] != largest && b[i] > secondlargest) // b[i] not equal to largest
  69. {
  70. secondlargest = b[i];
  71. }
  72. }
  73. b[maxIndex] = secondlargest; // mhnje secondlargest element la b[maxIndex] madhe store krt jaycha mhnje largestelement la secondlargest replace hott rahil
  74. count = count + 1; // counter increment karat jayacha
  75.  
  76. cout<<"count: "<<count;
  77. cout<<"final Array(Equal Values)";
  78. for(int i=0; i<n; i++)
  79. {
  80. cout<<b[i]<<" ";
  81. }
  82. cout<<endl;
  83. }
  84. }
  85. /*
  86. Output:
  87. count: 1final Array(Equal Values)2 2 1
  88. count: 2final Array(Equal Values)1 2 1
  89. count: 3final Array(Equal Values)1 1 1
  90.  */
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
count: 1final Array(Equal Values)2 2 1 
count: 2final Array(Equal Values)1 2 1 
count: 3final Array(Equal Values)1 1 1