fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13.  
  14. int k = 2;
  15. int arr[] = {1,3,2,1,4,5,3,2,1};
  16. int n = arr.length;
  17.  
  18. for(int i=0;i<n;i++){
  19. for(int j=i+1;j<n;j++){
  20. if(arr[i] == arr[j]){
  21. int diff = j - i;
  22. if(diff<=k){
  23. System.out.println("Same element occurred <=k distance");
  24. return;
  25. }
  26. }
  27. }
  28. }
  29. System.out.println("Same element doesn't occurred <=k distance");
  30.  
  31. }
  32. }
Success #stdin #stdout 0.07s 52508KB
stdin
Standard input is empty
stdout
Same element doesn't occurred <=k distance