# Smallest index such that there are no 0 or 1 to its right

Given a binary array of **N** numbers. The task is to find the smallest index such that there are either no 1’s or 0’s to the right of the index. **Note**: The array will have at least one 0 and one 1. **Examples:**

Input:a[] = {1, 1, 1, 0, 0, 1, 0, 1, 1}Output:6

At 6th index, there are no 0’s to the right of the index.Input:a[] = {0, 1, 0, 0, 0}Output:1

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the

DSA Self Paced Courseat a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please referComplete Interview Preparation Course.In case you wish to attend

live classeswith experts, please referDSA Live Classes for Working ProfessionalsandCompetitive Programming Live for Students.

**Approach**: Store the rightmost occurring index of both 1 and 0 and return the minimum of both.

Below is the implementation of the above approach:

## C++

`// C++ program to implement` `// the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the smallest index` `// such that there are no 0 or 1 to its right` `int` `smallestIndex(` `int` `a[], ` `int` `n)` `{` ` ` `// Initially` ` ` `int` `right1 = 0, right0 = 0;` ` ` `// Traverse in the array` ` ` `for` `(` `int` `i = 0; i < n; i++) {` ` ` `// Check if array element is 1` ` ` `if` `(a[i] == 1)` ` ` `right1 = i;` ` ` `// a[i] = 0` ` ` `else` ` ` `right0 = i;` ` ` `}` ` ` `// Return minimum of both` ` ` `return` `min(right1, right0);` `}` `// Driver code` `int` `main()` `{` ` ` `int` `a[] = { 1, 1, 1, 0, 0, 1, 0, 1, 1 };` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]);` ` ` `cout << smallestIndex(a, n);` ` ` `return` `0;` `}` |

## Java

`// Java program to implement` `// the above approach` `class` `GFG` `{` ` ` `// Function to find the smallest index` `// such that there are no 0 or 1 to its right` `static` `int` `smallestIndex(` `int` `[]a, ` `int` `n)` `{` ` ` `// Initially` ` ` `int` `right1 = ` `0` `, right0 = ` `0` `;` ` ` `// Traverse in the array` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++)` ` ` `{` ` ` `// Check if array element is 1` ` ` `if` `(a[i] == ` `1` `)` ` ` `right1 = i;` ` ` `// a[i] = 0` ` ` `else` ` ` `right0 = i;` ` ` `}` ` ` `// Return minimum of both` ` ` `return` `Math.min(right1, right0);` `}` `// Driver code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `[]a = { ` `1` `, ` `1` `, ` `1` `, ` `0` `, ` `0` `, ` `1` `, ` `0` `, ` `1` `, ` `1` `};` ` ` `int` `n = a.length;` ` ` `System.out.println(smallestIndex(a, n));` `}` `}` `// This code is contributed` `// by Code_Mech.` |

## Python3

`# Python 3 program to implement` `# the above approach` `# Function to find the smallest` `# index such that there are no` `# 0 or 1 to its right` `def` `smallestIndex(a, n):` ` ` ` ` `# Initially` ` ` `right1 ` `=` `0` ` ` `right0 ` `=` `0` ` ` `# Traverse in the array` ` ` `for` `i ` `in` `range` `(n):` ` ` ` ` `# Check if array element is 1` ` ` `if` `(a[i] ` `=` `=` `1` `):` ` ` `right1 ` `=` `i` ` ` `# a[i] = 0` ` ` `else` `:` ` ` `right0 ` `=` `i` ` ` `# Return minimum of both` ` ` `return` `min` `(right1, right0)` `# Driver code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `a ` `=` `[` `1` `, ` `1` `, ` `1` `, ` `0` `, ` `0` `, ` `1` `, ` `0` `, ` `1` `, ` `1` `]` ` ` `n ` `=` `len` `(a)` ` ` `print` `(smallestIndex(a, n))` ` ` `# This code is contributed by` `# Surendra_Gangwar` |

## C#

`// C# program to implement` `// the above approach` `using` `System;` `class` `GFG` `{` ` ` `// Function to find the smallest index` `// such that there are no 0 or 1 to its right` `static` `int` `smallestIndex(` `int` `[]a, ` `int` `n)` `{` ` ` `// Initially` ` ` `int` `right1 = 0, right0 = 0;` ` ` `// Traverse in the array` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `{` ` ` `// Check if array element is 1` ` ` `if` `(a[i] == 1)` ` ` `right1 = i;` ` ` `// a[i] = 0` ` ` `else` ` ` `right0 = i;` ` ` `}` ` ` `// Return minimum of both` ` ` `return` `Math.Min(right1, right0);` `}` `// Driver code` `public` `static` `void` `Main()` `{` ` ` `int` `[]a = { 1, 1, 1, 0, 0, 1, 0, 1, 1 };` ` ` `int` `n = a.Length;` ` ` `Console.Write(smallestIndex(a, n));` `}` `}` `// This code is contributed` `// by Akanksha Rai` |

## PHP

`<?php` `// PHP program to implement` `// the above approach` `// Function to find the smallest index` `// such that there are no 0 or 1 to its right` `function` `smallestIndex(` `$a` `, ` `$n` `)` `{` ` ` `// Initially` ` ` `$right1` `= 0; ` `$right0` `= 0;` ` ` `// Traverse in the array` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++)` ` ` `{` ` ` `// Check if array element is 1` ` ` `if` `(` `$a` `[` `$i` `] == 1)` ` ` `$right1` `= ` `$i` `;` ` ` `// a[i] = 0` ` ` `else` ` ` `$right0` `= ` `$i` `;` ` ` `}` ` ` `// Return minimum of both` ` ` `return` `min(` `$right1` `, ` `$right0` `);` `}` `// Driver code` `$a` `= ` `array` `(1, 1, 1, 0, 0, 1, 0, 1, 1);` `$n` `= sizeof(` `$a` `);` `echo` `smallestIndex(` `$a` `, ` `$n` `);` `// This code is contributed by Akanksha Rai` `?>` |

## Javascript

`<script>` `// Javascript program to implement` `// the above approach` ` ` `// Function to find the smallest index` ` ` `// such that there are no 0 or 1 to its right` ` ` `function` `smallestIndex(a, n)` ` ` `{` ` ` ` ` `// Initially` ` ` `let right1 = 0, right0 = 0;` ` ` `// Traverse in the array` ` ` `let i;` ` ` `for` `(i = 0; i < n; i++)` ` ` `{` ` ` `// Check if array element is 1` ` ` `if` `(a[i] == 1)` ` ` `right1 = i;` ` ` `// a[i] = 0` ` ` `else` ` ` `right0 = i;` ` ` `}` ` ` `// Return minimum of both` ` ` `return` `Math.min(right1, right0);` ` ` `}` ` ` ` ` `// Driver Code` ` ` ` ` `var` `a = [ 1, 1, 1, 0, 0, 1, 0, 1, 1 ];` ` ` `let n = a.length;` ` ` `document.write(smallestIndex(a, n));` ` ` ` ` `// This code is contributed by ajaykrsharma132.` `</script>` |

**Output:**

6

**Time Complexity**: O(N)