title: Is there a way to avoid an array allocation?
body: <p>I need cells index numbers, which fulfil following conditions:</p>
  
  <pre><code>    Q(i)&lt;=5 and V(i)/=1
  </code></pre>
  
  <p>(size(Q)==size(V)). I wrote something like this:</p>
  
  <pre><code>    program test
      implicit none
      integer, allocatable, dimension(:):: R 
      integer Q(5)
      integer V(5)
      integer counter,limit,i
  
       counter=0
       limit=5
       V=(/0,0,1,0,0/)
       Q=(/5,10,2,7,2/)
  
      do i=1,5
        if((Q(i)&lt;=5).AND.(V(i)/=1)) then
            counter=counter+1
        end if
      end do
  
      allocate(R(counter))
       counter=1
      do i=1,5
        if((Q(i)&lt;=5).AND.(V(i)/=1)) then
            R(counter)=i
            counter=counter+1
      end if
      end do 
      deallocate(R)
      end program test
  </code></pre>
  
  <p>but I don't think it is  a very efficient . Is there any better solution for this problem?</p>
  
  <p>I can remove one loop by writing</p>
  
  <pre><code>program test
    implicit none
    integer, allocatable, dimension(:):: R
    integer Q(5)
    integer V(5)
    integer counter,limit,i
  
    counter=0
    limit=5
    V=(/0,0,1,0,0/)
    Q=(/5,10,2,7,2/)
    V=-V+1
    allocate(R((count(V*Q&lt;=5)-count(V*Q==0))))
  
    counter=1
    do i=1,size(Q)
      if((Q(i)&lt;=5).AND.(V(i)==1)) then
        R(counter)=i
        counter=counter+1
      end if
    end do
  end program test
  </code></pre>