Facebook Twitter Instagram
    DeepCrazyWorld
    Facebook Twitter Instagram Pinterest YouTube
    • FLUTTER
      • FLUTTER APP
        • QRCode
        • Quiz App
        • Chat GPT
        • PDF App
        • News App
        • Fitness App
        • Weather App
        • BMI Calculator
        • GAME APP
        • Ecommerce App
        • wallpaper App
        • Finance app
        • Chat App
        • Wallet App
        • Taxi App
        • Quran app
        • Music player app
      • FLUTTER UI
        • Splash Screen
        • Onboarding Screen
        • Login Screen
        • Card Design
        • Drawer
    • PROJECT
      • Android Projects
      • College Projects
      • FLUTTER APP
      • Project Ideas
      • PHP Projects
      • Python Projects
    • SOURCE CODE
    • ANDROID
      • ANDROID APP
      • GAME APP
      • ANDROID STUDIO
    • MCQ
      • AKTU MCQ
        • RPA MCQ
        • COA MCQ
        • HPC MCQ
        • SPM MCQ
        • Renewable Energy All MCQ
        • Data Compression MCQ
        • Data Structure MCQ
        • Digital Image Processing MCQ
        • Software Engineering MCQ
        • Machine Learning MCQ
        • Artificial Intelligence MCQ
      • D PHARMA MCQ
        • Pharmaceutics – I MCQ
        • Pharmacognosy MCQ
        • Pharmaceutical Chemistry MCQ
        • Biochemistry and Clinical Pathology MCQ
        • Human Anatomy and Physiology MCQ
        • Heath Education and Community Pharmacy MCQ
    • INTERVIEW QUESTIONS
      • Flutter Interview Questions
      • INTERVIEW QUESTIONS
      • Python Interview Questions
      • Coding ninjas solution
    • MORE
      • WORDPRESS
        • SEO
        • TOP 10 WORDPRESS THEME
      • PRODUCTIVITY
      • Program
      • QUOTES
    DeepCrazyWorld
    Home»Coding ninjas solution»Coding Ninjas question – Given a graph with N vertices
    Coding ninjas solution

    Coding Ninjas question – Given a graph with N vertices

    DeepikaBy DeepikaOctober 8, 2023Updated:October 8, 2023No Comments3 Mins Read

    In this blog post we will Provide Coding Ninjas Basics of Java with Data Structures and Algorithms Solution. This will have solutions to all the problems that are included in Coding Ninjas Java Course.  Given a number N, print the following pattern Coding Ninjas question with solution. 

    Table of Contents

    Toggle
    • Coding Ninja question
    • Coding ninjas solutions
      • Java Language [Solutions]
    • Conclusion
        • Related Articles:
      • READ MORE

    Coding Ninja question

    <img decoding=
    Coding Ninjas question

    Given a graph with N vertices (numbered from 0 to N-1) and M undirected edges, then count the distinct 3-cycles in the graph. A 3-cycle PQR is a cycle in which (P,Q), (Q,R) and (R,P) are connected by an edge.


    Input Format :
    The first line of input contains two space separated integers, that denotes the value of N and M.
    Each of the following M lines contain two integers, that denote the vertices which have an undirected edge between them. Let us denote the two vertices with the symbol u and v.


    Output Format :
    Print the count the number of 3-cycles in the given graph
    Constraints :
    0 <= N <= 100
    0 <= M <= (N*(N-1))/2
    0 <= u <= N – 1
    0 <= v <= N – 1
    Time Limit: 1 sec
    Sample Input 1:
    3 3
    0 1
    1 2
    2 0


    Sample Output 1:
    1

    Coding ninjas solutions

    Java Language [Solutions]

    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class Solution {
    
        static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
        public static int solve(boolean[][] graph, int n) {
            int count=0;
            for(int i=0;i<graph.length;i++) {
                for(int j=0;j<graph.length;j++) {
                    if(graph[i][j] ==true) {
    
                        for(int k=0;k<graph.length;k++) {
    
                            if(k!=i && graph[k][j]==true && graph[i][k] ==true ) {
                                count++;
                            }
    
    
                        }
    
                    }
                }
            }
            return count/6; //here we are doing count/6, because we are not using visited array, 
            //so 1 cycle is counted 2 times for each of the three vertices, one in clockwise one in anti clockwise
    
        }
    
        public static boolean[][] takeInput() throws IOException {
            String[] strNums;
            strNums = br.readLine().split("\\s");
            int n = Integer.parseInt(strNums[0]);
            int m = Integer.parseInt(strNums[1]);
    
            boolean[][] graphs = new boolean[n][n];
            int firstvertex, secondvertex;
    
            for (int i = 0; i < m; i++) {
                String[] strNums1;
                strNums1 = br.readLine().split("\\s");
                firstvertex = Integer.parseInt(strNums1[0]);
                secondvertex = Integer.parseInt(strNums1[1]);
                graphs[firstvertex][secondvertex] = true;
                graphs[secondvertex][firstvertex] = true;
            }
            return graphs;
        }
    
        public static void main(String[] args) throws NumberFormatException, IOException {
            boolean[][] graphs = takeInput();
    
            int ans = solve(graphs, graphs.length);
            System.out.println(ans);
    
        }
    }

    Conclusion

    Coding Ninja primarily targets students and professionals looking to enhance their coding and programming skills, prepare for technical interviews, or pursue careers in software development and related fields. It has gained a reputation for its quality courses and experienced instructors, making it a popular choice among those looking to improve their coding abilities.

    Related Articles:

    • About coding ninja questions
    • Rectangular Numbers Coding Ninjas questions with solution
    • How to Install Flutter in windows 10
    • How to Setup Space Between Elements In Flutter 
    • Flutter Card Widget with Example
    • Integrating an API into a Flutter – Working with REST APIs
    • Create Login Page UI Design and Animation For Flutter

    READ MORE

    Share. Facebook Twitter LinkedIn WhatsApp Telegram Pinterest Reddit Email
    Previous ArticleRectangular Numbers Coding Ninjas question with solution
    Next Article Given a NxM matrix containing Uppercase – Coding Ninjas question

    Related Posts

    Given a NxM matrix containing Uppercase – Coding Ninjas question

    Coding ninjas solution 3 Mins Read

    Rectangular Numbers Coding Ninjas question with solution

    Coding Interview Questions 5 Mins Read

    Tell me about Coding Ninjas | Learn Coding, Programming Online

    Coding ninjas solution 3 Mins Read

    Top 30+ Coding Interview Questions with solution 2023 (updated)

    Coding Interview Questions 3 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • Implementing a Dynamic FAQ Screen UI in Flutter Using ExpansionTile March 29, 2025
    • Creating an Instruction UI Screen in Flutter Application March 29, 2025
    • Animated Backgrounds in Flutter: A Complete Guide March 15, 2025
    • How to make Diary App using flutter stepwise using getx August 31, 2024
    • How to Create Music Player UI screen with fully functional in flutter August 30, 2024
    • How to make ListView Builder Ui in flutter with Source Code August 29, 2024
    • Create a TabBar View in flutter with fully functional stepwise August 28, 2024
    • How to create TabBar view in flutter with source code step wise August 27, 2024
    • How to make Heart rate measure app with Flutter stepwise August 26, 2024
    • How to make ChatGpt App in flutter with source code Stepwise August 25, 2024
    Facebook Twitter Instagram Pinterest YouTube
    • About
    • Contact
    • Disclaimer
    • Privacy Policy
    Copyright by DeepCrazyWorld © 2025

    Type above and press Enter to search. Press Esc to cancel.