Lack of infrastructure, central controlling authority and the properties of wireless links make mobile ad hoc networks (MANETs) vulnerable to attacks. Several protocols have been proposed to make the routing protocols handle attacks in MANETs. These protocols detect the misbehaving nodes and re-route the data packets around them, mostly along the shortest such path. However, no single protocol handles all the attacks. A variant of the problem for routing around misbehaving nodes in ad hoc networks can be stated as: given a set of nodes under the danger of attack, one wishes to determine the path which is farthest from the endangered nodes. The problem does not address the problem of handling attack directly but tries to minimize the impact of attack. The problem also finds its applications in sensor networks. In this paper, we present a simple and efficient algorithm to solve the problem. The algorithm converges in $ O(d^2 ) $ time where d is the diameter of the network.