Codeforces 467A George and Accommodation Solution & Explanation

Difficulty : 800

Problem Description

George has recently entered the BSUCP (Berland State University for Cool Programmers). George has a friend Alex who has also entered the university. Now they are moving into a dormitory.

George and Alex want to live in the same room. The dormitory has n rooms in total. At the moment the i-th room has pi people living in it and the room can accommodate qi people in total (pi ≤ qi). Your task is to count how many rooms has free place for both George and Alex.

Input

The first line contains a single integer n (1 ≤ n ≤ 100) — the number of rooms.

The i-th of the next n lines contains two integers pi and qi (0 ≤ pi ≤ qi ≤ 100) — the number of people who already live in the i-th room and the room’s capacity.

Output

Print a single integer — the number of rooms where George and Alex can move in.

Eamples

Input3
1 1
2 2
3 3
Output0
Input3
1 10
0 10
10 10
Output2

Solution

In our solution, we need to iterate through all rooms and check if each room has at least two available spots for George and Alex to move in.

C# Solution

Solution1

int n = int.Parse(Console.ReadLine());  //total count of rooms 

int res = 0;

for(int i = 0; i<n ; i++)
{
    string[] arr  = Console.ReadLine().Split(' '); //split by space
    int alreadyUsed = int.Parse(arr[0]);
    int capacity = int.Parse(arr[1]);
    
    if(capacity-alreadyUsed >= 2){
        res+=1;
    }
}

Console.WriteLine(res);

Java Solution

Solution1

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine()); //total count of rooms 
        int res = 0;

        for (int i = 0; i < n; i++) 
        {
            String[] arr = scanner.nextLine().split(" ");
            int alreadyUsed = Integer.parseInt(arr[0]);
            int capacity = Integer.parseInt(arr[1]);
            
            if(capacity-alreadyUsed >= 2){
                res+=1;
            }
        }

        System.out.println(res);
    }
}

Conclusion

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts, thanks you~~

✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know !!

The problem link : Problem – 467A – Codeforces

Random Codeforces Posts

Leave a Reply

Your email address will not be published. Required fields are marked *