Skip to content

Instantly share code, notes, and snippets.

@bijay-shrestha
Created June 18, 2021 08:35
Show Gist options
  • Save bijay-shrestha/398667987758850c97125a0ae91878ba to your computer and use it in GitHub Desktop.
Save bijay-shrestha/398667987758850c97125a0ae91878ba to your computer and use it in GitHub Desktop.
package com.basic.practice;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class CheckNFor2N {
public static void main(String[] args) {
// int[] arrayOfNumber = {1, 6, 4, 5, 3};
int[] arrayOfNumber = {1, 9, 4, 5, 3};
log.info("Checking if for any number N, the number 2N is not in an array. Return 1 for Yes and 0 for No.", arrayOfNumber);
log.info("Actual Result: {}", checkNFor2N(arrayOfNumber));
}
static int checkNFor2N(int[] a) {
boolean notFound;
for (int i = 0; i < a.length; i++) {
notFound = true;
for (int j = 0; j < a.length; j++) {
if (a[i] == 2 * a[j]) {
notFound = false;
break;
}
}
if (!notFound) {
return 0;
}
}
return 1;
}
}
@sanjmgr
Copy link

sanjmgr commented Aug 19, 2022

private static int checkNFor2N(int[] arrayOfNumber) {

  for (int i = 0; i < arrayOfNumber.length; i++) {
      for (int j = 0; j < arrayOfNumber.length; j++) {
          if (arrayOfNumber[i] == 2 * arrayOfNumber[j]) {
              return 1;
          }
      }
  }

  return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment