- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Queries to check if it is possible to join boxes in a circle in C++
In this tutorial, we will be discussing a program to find queries to check if it is possible to join boxes in a circle.
For this we will be provided with a circle of boxes running from 1 to n. Our task is to find whether box i can be connected to box j with a rod without intersecting the previous rodes.
Example
#include <bits/stdc++.h> using namespace std; //checking if making a circle from boxes is possible void isPossible(int n, int q, int queryi[], int queryj[]) { int arr[50]; for (int i = 0; i <= n; i++) arr[i] = 0; for (int k = 0; k < q; k++) { int check = 0; if (queryj[k] < queryi[k]) { int temp = queryi[k]; queryi[k] = queryj[k]; queryj[k] = temp; } if (arr[queryi[k]] != 0 || arr[queryj[k]] != 0) check = 1; else if (queryi[k] == queryj[k]) check = 1; else { for (int i = 1; i < queryi[k]; i++) { if (arr[i] != 0 && arr[i] < queryj[k] && queryi[k] < arr[i]) { check = 1; break; } } if (check == 0) { for (int i = queryi[k] + 1; i < queryj[k]; i++) { if (arr[i] != 0 && arr[i] > queryj[k]) { check = 1; break; } } } } if (check == 0) { cout << "Possible" << endl; arr[queryi[k]] = queryj[k]; arr[queryj[k]] = queryi[k]; } else cout << "Not Possible" << endl; } } int main() { int size = 5; int q = 2; int queryi[] = { 3, 5 }; int queryj[] = { 1, 4 }; isPossible(size, q, queryi, queryj); return 0; }
Output
Possible Possible
Advertisements