Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmCount pieces of circle after N cuts

Count pieces of circle after N cuts

Given an integer N, where 1 \leq N \leq 10^{18}   . The task is to print the count of pieces of a circle with N cuts where each cut passes through the centre of given circle.
Examples
 

Input : N = 2
Output : 4

Input : N = 100
Output : 200

 

Approach: This problem can be easily solved with observation only. Since each cut passes through the centre, each cut creates two new pieces.
Let us see how above Intuition works. 
 

  • At first cut we have 2 different pieces of circle.
  • At second cut we have 2 new different pieces from previous 2 pieces of circle.
  • At third cut we have again 2 new different pieces from any of previous 2 pieces which are opposite to each other.

 

Circle with N cuts through center

In this way, we proceed with N cuts to get the count of total pieces after N cuts.
Below is the implementation of above approach: 
 

C++




// C++ program to find number of pieces
// of circle after N cuts
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find number of pieces
// of circle after N cuts
int countPieces(int N)
{
    return 2 * N;
}
 
// Driver program
int main()
{
    int N = 100;
 
    cout << countPieces(N);
 
    return 0;
}


Java




// Java program to find number of pieces
// of circle after N cuts
import java.util.*;
 
class solution
{
 
// Function to find number of pieces
// of circle after N cuts
static int countPieces(int N)
{
    return 2 * N;
}
 
// Driver program
public static void main(String args[])
{
    int N = 100;
 
    System.out.println(countPieces(N));
 
}
 
}


Python3




# Python program to find number
# of pieces of circle after N cuts
 
# Function to find number of
# pieces of circle after N cuts
def countPieces(N):
    return 2 * N
 
# Driver Code
N = 100
 
print(countPieces(N))
 
# This code is contributed by
# Sanjit_Prasad


C#




// C# program to find number of pieces
// of circle after N cuts
 
class solution
{
 
// Function to find number of pieces
// of circle after N cuts
static int countPieces(int N)
{
    return 2 * N;
}
 
// Driver program
static void Main()
{
    int N = 100;
 
    System.Console.WriteLine(countPieces(N));
 
}
 
}
// This code is contributed by mits


PHP




<?php
// PHP program to find number of
// pieces of circle after N cuts
 
// Function to find number of pieces
// of circle after N cuts
function countPieces($N)
{
    return 2 * $N;
}
 
// Driver Code
$N = 100;
 
echo countPieces($N);
 
// This code is contributed by anuj_67
?>


Javascript




<script>
 
// Javascript program to find number of pieces
// of circle after N cuts
 
// Function to find number of pieces
// of circle after N cuts
function countPieces(N)
{
    return 2 * N;
}
 
// driver program
     
    let N = 100;
 
    document.write(countPieces(N));
    
</script>


Output: 

200

 

Time Complexity: O(1)

Auxiliary Space: O(1)
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nango Kalahttps://www.kala.co.za
Experienced Support Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Microsoft Excel, Customer Service, Microsoft Word, Technical Support, and Microsoft Office. Strong information technology professional with a Microsoft Certificate Solutions Expert (Privet Cloud) focused in Information Technology from Broadband Collage Of Technology.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments