fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int n;
  7. cin>>n;
  8.  
  9. int a[n+1];// 1-based indexing
  10. for(int i=1; i<=n; i++)
  11. {
  12. cin>>a[i];
  13. }
  14.  
  15. int b[n+1];
  16. for(int i=1; i<=n; i++)
  17. {
  18. cin>>b[i];
  19. }
  20.  
  21. int r = 0;
  22. int c = 0;
  23. int count = 0; // longest subarray which is valid starting at index 'i'
  24. for(int i=1; i<=n; i++)
  25. {
  26. c = 1; // reset c and start from b[1]
  27. count = 0; // reset count
  28.  
  29. for(int j=i; j<=n; j++)
  30. {
  31.  
  32. //a[i....j]
  33. if(a[j]==b[c])
  34. {
  35. count++; // count the matching elements
  36. c++; // move to next element of b
  37. }
  38. else
  39. {
  40. c++; // else increment c++ the pointer in 'B' Array Na Kyuki uspe subset nahi mil raha vo wala Array 'A' ka
  41. }
  42. }
  43. r = max(r,count);
  44. }
  45.  
  46. r = n - r; // convert maximum matching elements into operations
  47.  
  48. cout<<"Number of operations needed to make Array 'B' equal to Array 'A' : "<<r;
  49.  
  50. return 0;
  51.  
  52. /*
  53.   5
  54.   1 2 3 4 5
  55.   2 3 4 5 1
  56.  
  57.   otput: 1
  58.   */
  59. }
Success #stdin #stdout 0.01s 5324KB
stdin
5
1 2 3 4 5 
2 3 4 5 1
stdout
Number of operations needed to make Array 'B' equal to Array 'A' : 1