Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3278830
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:31:27+00:00 2026-05-17T19:31:27+00:00

The following is the problem I’m working on and my snippet of code. Is

  • 0

The following is the problem I’m working on and my snippet of code.
Is there a better way to implement this? I have used basic control structures for this below.

Is it better to store the rows and columns in a map and searching through the map based on the key/value pairs?

There is a security keypad at the entrance of a building. It has 9 numbers 1 – 9 in a 3×3 matrix format.

1 2 3
4 5 6
7 8 9

The security has decided to allow one digit error for a person but that digit should be horizontal or vertical. Example: for 5 the user is allowed to enter 2, 4, 6, 8 or for 4 the user is allowed to enter 1, 5, 7. IF the security code to enter is 1478 and if the user enters 1178 he should be allowed.

The following is a snippet of code i was working on:

ArrayList<Integer> list = new ArrayList<Integer>();
int num = 9;
int[][] arr = {{1,2,3},{4,5,6},{7,8,9}};

for(int i =0;i< arr.length;i++){
  for(int j = 0; j <arr.length;j++){
    if(num == arr[i][j]){
      row = i;
      col = j;
      break;

    }
  }
}
for(int j1 = 0; j1< 3 ; j1++){
  if(arr[row][j1] != num){
    list.add(arr[row][j1]);
  }
}
for(int i1 = 0 ; i1 <3;i1++){
  if(arr[i1][col] != num){
    list.add(arr[i1][col]);
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:31:28+00:00Added an answer on May 17, 2026 at 7:31 pm

    There are many ways to solve this, but I think it can be solved with HashMaps and HashSets more efficiently than doing several iterations.

    If I were you, I would build the data model first using a hash map and a hash set. This is because hash map and hash set have fast lookup, (no iterations)

    HashMap<Integer,HashSet<Integer>> values = new HashMap<Integer, HashSet<Integer>>();
    
    //now put in the accepted values for one
    HashSet<Integer> oneValues = new HashSet<Integer>();
    oneValues.put(1);
    oneValues.put(2);
    oneValues.put(4);
    values.put(1, oneValues);
    
    //put in 2 values
    ......
    

    Then when you parse your input, if you want to see if an inputed value is accepted for what the code is, just do something like

    private boolean isAccepted(int input, int combinationValue)
    {
      // check to see if the inputed value in the accepted values set
      return values.get(combinationValue).contains(input);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following problem: Website build with PHP/HTML working We want to change this site to
i have following problem To decode the Biquinary code use the number 5043210. At
I have the following problem: Suppose I have some basic counter class Counter .
I have the following problem. There's a pagecontrol on my form and I want
I have the following problem. I have to implement a class that has an
I have the following problem using subversion: I'm currently working on the trunk of
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
The following problem has me stumped: I have a WinForms application which used to
I have the following problem. I have embedded a map using Google Maps' API.
I am having the following problem: a) I have a UNIX build environment set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.