Given an array **arr[]** and an integer **X**, the task is to check that maximum difference between indices of the non-zero elements is greater than or equal to **X****Examples:**

Input:arr[] = {1, 0, 1}, X = 3Output:NoExplanation:

Maximum Difference between the indices of non-zero elements is 2.

Input:arr[] = {1, 0, 0, 0, 0, 0, 1}, X = 6Output:YesExplanation:

Maximum Difference between the indices of non-zero elemente is 6.

**Approach:** The idea is to maintain the **occurrence of last non-zero element** in the array and As soon as there is a new element which is non-zero then compare the distance between the last occurrence index to the current index. If the difference between them is greater than or equal to X. Then, update the last occurrence of the non-zero element and continue checking. Otherwise, return False.

Below is the implementation of the above approach:

## C++

`// C++ implementation to check that ` `// maximum difference of indices between ` `// non-zero elements if greater than X ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check that maximum ` `// difference of indices between ` `// non-zero elements if greater than X ` `void` `findRuleFollowed(` `int` `arr[], ` `int` `n, ` `int` `x) ` `{ ` ` ` `int` `last_occur = -1; ` ` ` `bool` `flag = ` `true` `; ` ` ` ` ` `// Loop to iterate over the elements ` ` ` `// of the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` ` ` `// Condition if there is a last occured ` ` ` `// non-zero element in the array ` ` ` `if` `(arr[i] != 0 && last_occur != -1) ` ` ` `{ ` ` ` `int` `diff = i - last_occur; ` ` ` `if` `(diff >= x) ` ` ` `{ ` ` ` `continue` `; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `flag = ` `false` `; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` `else` `if` `(arr[i] != 0 && last_occur == -1) ` ` ` `{ ` ` ` `last_occur = i; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Condition to check if the ` ` ` `// maximum difference is maintained ` ` ` `if` `(flag) ` ` ` `cout << ` `"YES"` `; ` ` ` `else` ` ` `cout << ` `"NO"` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 0, 1, 0, 0, 1 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(0); ` ` ` `int` `x = 3; ` ` ` ` ` `// Function call ` ` ` `findRuleFollowed(arr, n, x); ` `} ` ` ` `// This code is contributed by ankitkumar34 ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to check that ` `// maximum difference of indices between ` `// non-zero elements if greater than X ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to check that maximum ` `// difference of indices between ` `// non-zero elements if greater than X ` `static` `void` `findRuleFollowed(` `int` `arr[], ` `int` `n, ` `int` `x) ` `{ ` ` ` `int` `last_occur = -` `1` `; ` ` ` `boolean` `flag = ` `true` `; ` ` ` ` ` `// Loop to iterate over the elements ` ` ` `// of the array ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` ` ` `// Condition if there is a last occured ` ` ` `// non-zero element in the array ` ` ` `if` `(arr[i] != ` `0` `&& last_occur != -` `1` `) ` ` ` `{ ` ` ` `int` `diff = i - last_occur; ` ` ` `if` `(diff >= x) ` ` ` `{ ` ` ` `continue` `; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `flag = ` `false` `; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` `else` `if` `(arr[i] != ` `0` `&& last_occur == -` `1` `) ` ` ` `{ ` ` ` `last_occur = i; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Condition to check if the ` ` ` `// maximum difference is maintained ` ` ` `if` `(flag) ` ` ` `System.out.println(` `"YES"` `); ` ` ` `else` ` ` `System.out.println(` `"NO"` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `arr[] = { ` `0` `, ` `1` `, ` `0` `, ` `0` `, ` `1` `}; ` ` ` `int` `n = arr.length; ` ` ` `int` `x = ` `3` `; ` ` ` ` ` `// Function call ` ` ` `findRuleFollowed(arr, n, x); ` `} ` `} ` ` ` `// This code is contributed by ankitkumar34 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to check that ` `# maximum difference of indices between ` `# non-zero elements if greater than X ` ` ` `# Function to check that ` `# maximum difference of indices between ` `# non-zero elements if greater than X ` `def` `findRuleFollowed(arr, x): ` ` ` `last_occur ` `=` `-` `1` ` ` `flag ` `=` `True` ` ` ` ` `# Loop to iterate over the elements ` ` ` `# of the array ` ` ` `for` `i ` `in` `range` `(` `len` `(arr)): ` ` ` ` ` `# Condition if there is a last occured ` ` ` `# non-zero element in the array ` ` ` `if` `arr[i] !` `=` `0` `and` `last_occur !` `=` `-` `1` `: ` ` ` `diff ` `=` `i ` `-` `last_occur ` ` ` `if` `diff >` `=` `x: ` ` ` `continue` ` ` `else` `: ` ` ` `flag ` `=` `False` ` ` `break` ` ` `elif` `arr[i] !` `=` `0` `and` `last_occur ` `=` `=` `-` `1` `: ` ` ` `last_occur ` `=` `i ` ` ` ` ` `# Condition to check if the ` ` ` `# maximum difference is maintained ` ` ` `if` `flag: ` ` ` `print` `(` `"YES"` `) ` ` ` `else` `: ` ` ` `print` `(` `"NO"` `) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `arr ` `=` `[` `0` `, ` `1` `, ` `0` `, ` `0` `, ` `1` `] ` ` ` `x ` `=` `3` ` ` ` ` `# Function Call ` ` ` `findRuleFollowed(arr, x) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to check that ` `// maximum difference of indices between ` `// non-zero elements if greater than X ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to check that maximum ` `// difference of indices between ` `// non-zero elements if greater than X ` `static` `void` `findRuleFollowed(` `int` `[]arr, ` ` ` `int` `n, ` `int` `x) ` `{ ` ` ` `int` `last_occur = -1; ` ` ` `bool` `flag = ` `true` `; ` ` ` ` ` `// Loop to iterate over the elements ` ` ` `// of the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` ` ` `// Condition if there is a last occured ` ` ` `// non-zero element in the array ` ` ` `if` `(arr[i] != 0 && last_occur != -1) ` ` ` `{ ` ` ` `int` `diff = i - last_occur; ` ` ` ` ` `if` `(diff >= x) ` ` ` `{ ` ` ` `continue` `; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `flag = ` `false` `; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` `else` `if` `(arr[i] != 0 && last_occur == -1) ` ` ` `{ ` ` ` `last_occur = i; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Condition to check if the ` ` ` `// maximum difference is maintained ` ` ` `if` `(flag) ` ` ` `Console.WriteLine(` `"YES"` `); ` ` ` `else` ` ` `Console.WriteLine(` `"NO"` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String []args) ` `{ ` ` ` `int` `[]arr = { 0, 1, 0, 0, 1 }; ` ` ` `int` `n = arr.Length; ` ` ` `int` `x = 3; ` ` ` ` ` `// Function call ` ` ` `findRuleFollowed(arr, n, x); ` `} ` `} ` ` ` `// This code is contributed by Amit Katiyar` |

*chevron_right*

*filter_none*

**Output:**

YES

**Time Complexity:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Smallest subarray of size greater than K with sum greater than a given value
- Number of indices pair such that element pair sum from first Array is greater than second Array
- Maximize the number of indices such that element is greater than element to its left
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Count of elements whose absolute difference with the sum of all the other elements is greater than k
- Sum of all array elements less than X and greater than Y for Q queries
- Subarray with difference between maximum and minimum element greater than or equal to its length
- Maximum number of elements greater than X after equally distributing subset of array
- Count elements such that there are exactly X elements with values greater than or equal to X
- Merge first two minimum elements of the array until all the elements are greater than K
- Reduce the array by deleting elements which are greater than all elements to its left
- Count of Array elements greater than or equal to twice the Median of K trailing Array elements
- Count of array elements which are greater than all elements on its left
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Longest subarray in which absolute difference between any two element is not greater than X
- Split Array into min number of subsets with difference between each pair greater than 1
- Maximum sub-sequence sum such that indices of any two adjacent elements differs at least by 3
- Maximum difference of indices (i, j) such that A[i][j] = 0 in the given matrix

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.