Longest prefix that contains same number of X and Y in an array

Given two positive integers X and Y, and an array arr[] of positive integers. We need to find the longest prefix index which contains an equal number of X and Y. Print the maximum index of largest prefix if exist otherwise print -1.

Examples:

Input : array[] = [7, 42, 5, 6, 42, 8, 7, 5, 3, 6, 7]
        X = 7 Y = 42
Output : 9
The longest prefix with same number of occurrences 
of 7 and 42 is:
7, 42, 5, 6, 42, 8, 7, 5, 3, 6 42



We start from the index 0 and run a loop till the end of array. We keep increasing counters for both numbers X and Y. After iterating over the whole array, the last index when counts of X and y were equal is our result.

Below is the implementation of above idea :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find longest prefix with
// equal numbers of X and Y.
#include<bits/stdc++.h>
using namespace std;
  
// Function to find the max index of largest
// prefix with same number if X and Y
int findIndex(int arr[], int X, int Y, int n)
{
    // counters for X and Y
    int nx = 0,ny = 0;
  
    int result = -1;
    for (int i=0; i<n; i++)
    {
        // If value is equal to X increment counter of X
        if (arr[i]==X)
            nx++;
  
        // If value is equal to Y increment counter of Y
        if (arr[i]==Y)
            ny++;
  
        // If counters are equal(but not zero) save
        // the result as i
        if ((nx != 0) && (nx == ny))
            result = i;
    }
    return result;
}
  
// Driver code
int main()
{
    int arr[] = {7, 42, 5, 6, 42, 8, 7, 5, 3, 6, 7};
    int X = 7, Y = 42;
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << "Ending index of prefix is "
         << findIndex(arr, X, Y, n);
  
    return 0;
}
// Improved by Qu Dongfang.

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find longest 
// prefix with equal numbers
// of X and Y.
import java.io.*;
  
public class GFG
{
      
// Function to find the max index 
// of largest prefix with same
// number if X and Y
static int findIndex(int []arr, int X,
                     int Y, int n)
{
      
    // counters for X and Y
    int nx = 0, ny = 0;
  
    int result = -1;
    for (int i = 0; i < n; i++)
    {
        // If value is equal to X 
        // increment counter of X
        if (arr[i] == X)
            nx++;
  
        // If value is equal to Y
        // increment counter of Y
        if (arr[i] == Y)
            ny++;
  
        // If counters are equal(but not
        // zero) save the result as i
        if ((nx != 0) && (nx == ny))
            result = i;
    }
      
    return result;
}
  
    // Driver code
    static public void main (String[] args)
    {
    int []arr = {7, 42, 5, 6, 42, 8
                 7, 5, 3, 6, 7};
    int X = 7, Y = 42;
    int n = arr.length;
    System.out.println("Ending index of prefix is "
        + findIndex(arr, X, Y, n));
    }
}
  
// This code is contributed by vt_m. 
// Improved by Qu Dongfang.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to find longest prefix 
# with equal numbers of X and Y.
  
# Function to find the max index of
# largest prefix with same number
# if X and Y
def findIndex(arr, X, Y, n):
      
    # counters for X and Y
    nx = 0
    ny = 0
  
    result = -1
    for i in range(n):
          
        # If value is equal to X increment 
        # counter of X
        if arr[i] == X:
            nx += 1
  
        # If value is equal to Y increment 
        # counter of Y
        if arr[i] == Y:
            ny += 1
  
        # If counters are equal(but not zero)
        # save the result as i
        if nx and (nx == ny):
            result = i
  
    return result
  
# Driver code
if __name__ == '__main__':
    arr = [7, 42, 5, 6, 42, 8, 7, 5, 3, 6, 7]
    X = 7
    Y = 42
    n = len(arr)
    print("Ending index of prefix is",
              findIndex(arr, X, Y, n))
  
# This code is contributed by
# Sanjit_Prasad
# Improved by Qu Dongfang.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find longest 
// prefix with equal numbers
// of X and Y.
using System;
  
public class GFG
{
      
// Function to find the max index 
// of largest prefix with same
// number if X and Y
static int findIndex(int []arr, int X,
                     int Y, int n)
{
      
    // counters for X and Y
    int nx = 0, ny = 0;
  
    int result = -1;
    for (int i = 0; i < n; i++)
    {
        // If value is equal to X 
        // increment counter of X
        if (arr[i] == X)
            nx++;
  
        // If value is equal to Y
        // increment counter of Y
        if (arr[i] == Y)
            ny++;
  
        // If counters are equal(but not
        // zero) save the result as i
        if ((nx != 0) && (nx == ny))
            result = i;
    }
      
    return result;
}
  
    // Driver code
    static public void Main (String []args)
    {
    int []arr = {7, 42, 5, 6, 42, 8, 
                 7, 5, 3, 6, 7};
    int X = 7, Y = 42;
    int n = arr.Length;
    Console.WriteLine("Ending index of prefix is "
        + findIndex(arr, X, Y, n));
    }
}
  
// This code is contributed by vt_m.
// Improved by Qu Dongfang.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find longest prefix with
// equal numbers of X and Y.
  
// Function to find the max index of largest
// prefix with same number if X and Y
function findIndex($arr, $X, $Y, $n)
{
    // counters for X and Y
    $nx = 0; $ny = 0;
  
    $result = -1;
    for ($i = 0; $i < $n; $i++)
    {
        // If value is equal to X increment
        // counter of X
        if ($arr[$i] == $X)
            $nx++;
  
        // If value is equal to Y increment
        // counter of Y
        if ($arr[$i] == $Y)
            $ny++;
  
        // If counters are equal(but not zero) 
        // save the result as i
        if (($nx != 0) && ($nx == $ny))
            $result = $i;
    }
    return $result;
}
  
// Driver code
$arr = array(7, 42, 5, 6, 42, 8, 
                  7, 5, 3, 6, 7);
$X = 7; $Y = 42;
$n = sizeof($arr);
echo "Ending index of prefix is "
       findIndex($arr, $X, $Y, $n);
  
// This code is contributed 
// by Akanksha Rai
// Improved by Qu Dongfang.
?>

chevron_right



Output:

Ending index of prefix is 9

Time complexity : O(n)

Reference :
https://www.careercup.com/question?id=6194336212451328

This article is contributed by Niteesh Kumar. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.