From cd58de02de549d2d26452fa238b9a50844a67ba2 Mon Sep 17 00:00:00 2001 From: KARTIKESH SINGH Date: Sat, 26 Oct 2024 13:51:36 +0530 Subject: [PATCH] Added Remainder-Algorthim --- Algorithms/Remainder_Algorithm.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Algorithms/Remainder_Algorithm.cpp diff --git a/Algorithms/Remainder_Algorithm.cpp b/Algorithms/Remainder_Algorithm.cpp new file mode 100644 index 0000000..821d160 --- /dev/null +++ b/Algorithms/Remainder_Algorithm.cpp @@ -0,0 +1,30 @@ +// This algorithm helps to add two values to the element without using extra space +#include +using namespace std; + +// Dividend = Divisor * Quotient + Remainder; +// So when we divide dividend by Quotient later, we will obtain Divisor. +// So, we will first modify our arr[i] as +// arr[i] = (arr[arr[i]]%n)*n + arr[i]; + +class Solution{ + public: + // arr: input array + // n: size of array + //Function to rearrange an array so that arr[i] becomes arr[arr[i]] + //with O(1) extra space. + void arrange(long long arr[], int n) { + for(int i =0;i