Number of times path is crossed
Last Updated :
14 Sep, 2023
Given an array arr[] of size N, you are standing on origin (0, 0). You are allowed to move arr[0] units in the left direction, then arr[1] units in the up direction, and so on, i.e. after every arr[i] distance moved, you can move arr[i+1] in the next clockwise direction. Find the number of times you cross your own traveled path.
Examples:
Input: arr[] = {2, 1, 2, 1, 1}
Output: 1
Explanation: We are starting from coordinate (0, 0)
- Our first move will be in a left direction so moving 2 units in the left direction we will be at coordinate (0, -2).
- On the second move we will move 1 unit in the up direction and come at coordinate (-1, 2).
- On the third move we will move 2 units in the right direction and come at coordinate (-1, 0).
- On the fourth move we will move 1 unit in the down direction and come at coordinate (0, 0) and this coordinate is previously visited means at this point we are crossing our own traveled path.
- On the fifth move we will move 1 unit in the left direction and come at coordinate (0, -1).
Approach: This can be solved with the following idea:
Since there will be only 4 directions to move so, we can find the direction by computing i modulo 4.
- if i%4=0 we will move in the left direction.
- if i%4=1 we will move in the up direction.
- if i%4=2 we will move in the right direction.
- if i%4=3 we will move in the down direction.
Now, consider our current position as (x, y) in a 2-D matrix. Now, according to the basic logic of a 2-D matrix
- if we move some distance in the left direction our position will become (x – distance moved, y).
- if we move some distance in an up direction our position will become (x, y + distance moved).
- if we move some distance in the right direction our position will become (x + distance moved, y).
- if we move some distance in a down direction our position will become (x, y – distance moved).
Follow the steps mentioned below to implement the approach:
- Initialize the set of pairs so that to store the computed coordinates and also declare three variables x, y, and path_crossed, and initialize variables to 0.
- Insert pair of x and y in the set.
- We will run a loop from 0 to n- 1, where n is the size of the array.
- For each i in the range from 0 to n – 1 we will calculate the value of the new coordinate visited by moving arr[i] units (calculation is done using the methods discussed in the approach).
- Check if the current coordinate is present in the set. If the current coordinate is present then increment the variable path_crossed by one.
- Insert the current coordinate in the set.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int numberOfTimesPathCrossed( int arr[], int n)
{
set<pair< int , int > > coordinate;
int x = 0, y = 0, path_crossed = 0;
coordinate.insert({ x, y });
for ( int i = 0; i < n; i++) {
if (i % 4 == 0) {
x = x - arr[i];
}
else if (i % 4 == 1) {
y = y + arr[i];
}
else if (i % 4 == 2) {
x = x + arr[i];
}
else if (i % 4 == 3) {
y = y - arr[i];
}
if (coordinate.find({ x, y }) != coordinate.end())
path_crossed++;
}
coordinate.insert({ x, y });
return path_crossed;
}
int main()
{
int arr[] = { 2, 1, 2, 1, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
int answer = numberOfTimesPathCrossed(arr, n);
cout << answer << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.HashSet;
import java.util.Set;
public class GFG {
private static int numberOfTimesPathCrossed( int [] arr) {
Set<Pair> coordinate = new HashSet<>();
int x = 0 , y = 0 , path_crossed = 0 ;
coordinate.add( new Pair(x, y));
for ( int i = 0 ; i < arr.length; i++) {
if (i % 4 == 0 ) {
x = x - arr[i];
}
else if (i % 4 == 1 ) {
y = y + arr[i];
}
else if (i % 4 == 2 ) {
x = x + arr[i];
}
else if (i % 4 == 3 ) {
y = y - arr[i];
}
if (coordinate.contains( new Pair(x, y))) {
path_crossed++;
}
}
coordinate.add( new Pair(x, y));
return path_crossed;
}
private static class Pair {
int x;
int y;
Pair( int x, int y) {
this .x = x;
this .y = y;
}
@Override
public boolean equals(Object obj) {
if ( this == obj) return true ;
if (obj == null || getClass() != obj.getClass()) return false ;
Pair pair = (Pair) obj;
return x == pair.x && y == pair.y;
}
@Override
public int hashCode() {
return Integer.hashCode(x) ^ Integer.hashCode(y);
}
}
public static void main(String[] args) {
int [] arr = { 2 , 1 , 2 , 1 , 1 };
int answer = numberOfTimesPathCrossed(arr);
System.out.println(answer);
}
}
|
Python3
def number_of_times_path_crossed(arr):
coordinate = set ()
x, y = 0 , 0
path_crossed = 0
coordinate.add((x, y))
for i in range ( len (arr)):
if i % 4 = = 0 :
x - = arr[i]
elif i % 4 = = 1 :
y + = arr[i]
elif i % 4 = = 2 :
x + = arr[i]
elif i % 4 = = 3 :
y - = arr[i]
if (x, y) in coordinate:
path_crossed + = 1
coordinate.add((x, y))
return path_crossed
arr = [ 2 , 1 , 2 , 1 , 1 ]
answer = number_of_times_path_crossed(arr)
print (answer)
|
C#
using System;
using System.Collections.Generic;
class Program
{
static int NumberOfTimesPathCrossed( int [] arr, int n)
{
HashSet<Tuple< int , int >> coordinate = new HashSet<Tuple< int , int >>();
int x = 0, y = 0, pathCrossed = 0;
coordinate.Add( new Tuple< int , int >(x, y));
for ( int i = 0; i < n; i++)
{
if (i % 4 == 0)
{
x = x - arr[i];
}
else if (i % 4 == 1)
{
y = y + arr[i];
}
else if (i % 4 == 2)
{
x = x + arr[i];
}
else if (i % 4 == 3)
{
y = y - arr[i];
}
if (coordinate.Contains( new Tuple< int , int >(x, y)))
pathCrossed++;
}
coordinate.Add( new Tuple< int , int >(x, y));
return pathCrossed;
}
static void Main( string [] args)
{
int [] arr = { 2, 1, 2, 1, 1 };
int n = arr.Length;
int answer = NumberOfTimesPathCrossed(arr, n);
Console.WriteLine(answer);
}
}
|
Javascript
function numberOfTimesPathCrossed(arr) {
let coordinate = new Set();
let x = 0, y = 0, path_crossed = 0;
coordinate.add(`${x},${y}`);
for (let i = 0; i < arr.length; i++) {
if (i % 4 === 0) {
x = x - arr[i];
}
else if (i % 4 === 1) {
y = y + arr[i];
}
else if (i % 4 === 2) {
x = x + arr[i];
}
else if (i % 4 === 3) {
y = y - arr[i];
}
if (coordinate.has(`${x},${y}`)) {
path_crossed++;
}
}
coordinate.add(`${x},${y}`);
return path_crossed;
}
let arr = [2, 1, 2, 1, 1];
let answer = numberOfTimesPathCrossed(arr);
console.log(answer);
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(N2)
Share your thoughts in the comments
Please Login to comment...