fork download
  1. # Define the array
  2. arr = [1.5, 2.3, 1.1, 0.9]
  3. # Initialize the counter
  4. count = 0
  5. # Initialize the list to store indices
  6. indices = []
  7.  
  8. # Iterate through the array, excluding the last element
  9. for i in range(len(arr) - 1):
  10. # If the current element is greater than its right - hand neighbor
  11. if arr[i] > arr[i + 1]:
  12. # Add the index of the current element to the indices list
  13. indices.append(i)
  14. # Increment the counter by 1
  15. count += 1
  16.  
  17. # Print the indices of elements greater than their right - hand neighbors and the corresponding element values
  18. print(f"In the array {arr}, the indices {indices} correspond to elements {[arr[idx] for idx in indices]} which are greater than their right - hand neighbors.")
  19. # Print the number of such elements
  20. print(f"In the array {arr}, the number of such elements (K) is {count}.")
Success #stdin #stdout 0.1s 14172KB
stdin
Standard input is empty
stdout
In the array [1.5, 2.3, 1.1, 0.9], the indices [1, 2] correspond to elements [2.3, 1.1] which are greater than their right - hand neighbors.
In the array [1.5, 2.3, 1.1, 0.9], the number of such elements (K) is 2.