0
    //initialization
    int N,chk=1,div=1,count=0;

    //accepting input
    N = s.nextInt();

    //initializing array
    int [] a = new int[N];


    for (int i=0;i<N;i++ )
        a[i] = s.nextInt();

    while(div==1) {
        for (int i=0;i<N ;i++ ) {
            if(chk%a[i]==0){  //any array element could divide chk value
                ++chk;
                i=-1;
                count=0;
            }else                                          
            count++;
        }
        if(count==N){ //if all array elemet could not divide the chk value
            div=0;
        }
        else
            div=1;
    }

    System.out.println(chk);

sample input one:
3

3 5 7

output:
1

explaination: smallest natural number not divisible by all 3 integers

explaination: smallest natural number not divisible by all 3 integers

shazam
  • 1
  • 2
  • I don't know if this is intentional or not, but you have the following in your loop: i=-1 which means you're setting I to be -1. If you're intending that I be one less, do I--; – Kwright02 May 06 '18 at 01:37
  • @superPhreshHackerKid according to you error of ArrayIndexOutOfBound should appear,which is not happening. – shazam May 06 '18 at 01:51
  • @Kwright02 i am not intending for i to be one less, i want to set i to 0 so that on next iteration a[0] should be accessed. – shazam May 06 '18 at 01:53
  • then do the following: i = 0; – Kwright02 May 06 '18 at 01:57
  • @Kwright02 on doing i=0; array element with index 0 will never be accessed. BTW on doing i=0; same output. – shazam May 06 '18 at 02:01
  • true forgot about that at the moment. So are you basically trying to generate 3 random numbers, and go up linearly with a 4th number until the other 3 numbers can't be divided by that number? Meaning int a,b,c; are 3 random numbers. and we're going to increment d up from 1 until that number can't go into a b and c – Kwright02 May 06 '18 at 02:02
  • @Kwright02 no, i am trying to find the (single)minimum number which is not divisible by all the three numbers. refer to the input and output format – shazam May 06 '18 at 02:08
  • Ah I see, you'd probably have to do some searching on the modulous operator. – Kwright02 May 06 '18 at 02:11
  • @superPhreshHackerKid I didn't quite get you. can you tell me hoe to solve it? – shazam May 06 '18 at 02:12
  • @shazam, Sorry again, the algorithm threw me off with its implementation. I just worked it out on paper... Not sure why its getting hung up – superPhreshHackerKid May 06 '18 at 02:21
  • @superPhreshHackerKid i got the answer, it was a silly one though. The problem was this that if i have 1(single number) as my array element the if condition will never be false because any increment of chk will result in zero because any natural number modulo 1 will result in zero. – shazam May 06 '18 at 02:26
  • @shazam, Yep. I kept thinking it would return the value you checked against, good catch. – superPhreshHackerKid May 06 '18 at 02:28

0 Answers0