<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Rishabh Pagaria]]></title><description><![CDATA[Rishabh Pagaria]]></description><link>https://rishabhpagaria.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Thu, 17 Sep 2026 09:37:17 GMT</lastBuildDate><atom:link href="https://rishabhpagaria.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Reverse the Array]]></title><description><![CDATA[Introduction
We are given a  n  size array and you have to reverse the array using O(N) time complexity. So, we would try to solve 2 questions here :

Reverse the array

Reverse the array after position  m 


The size of the given array is n = 6

App...]]></description><link>https://rishabhpagaria.hashnode.dev/reverse-the-array</link><guid isPermaLink="true">https://rishabhpagaria.hashnode.dev/reverse-the-array</guid><category><![CDATA[General Programming]]></category><category><![CDATA[data structures]]></category><category><![CDATA[array]]></category><category><![CDATA[C++]]></category><dc:creator><![CDATA[Rishabh Pagaria]]></dc:creator><pubDate>Sun, 16 Jan 2022 14:21:25 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1642342599130/d1WuqGaYR.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1 id="heading-introduction">Introduction</h1>
<p>We are given a <strong> n </strong> size array and you have to reverse the array using O(N) time complexity. So, we would try to solve 2 questions here :</p>
<ul>
<li><p>Reverse the array</p>
</li>
<li><p>Reverse the array after position <strong> m </strong></p>
</li>
</ul>
<p>The size of the given array is n = 6</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1642325200806/nYx1IgETD7.png" alt="cv.png" /></p>
<h1 id="heading-approach">Approach</h1>
<p>The simplest approach which any programmer could think of is (including me also :) ) the brute force approach, i.e. Traversing the array from the back and then printing the array one by one.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1642325208777/Fd9RjtRTU.png" alt="fvfv.png" /></p>
<p>Starting from i = 5 and we will go till i = 0 and voila we got our reverse array.</p>
<p>Time Complexity : <strong> O(n) </strong> because we are traversing from i = 5 till i = 0, if we had n sized array then we would have traversed from i = n till i = 0. Therefore the time complexity is <strong> O(n) </strong>.</p>
<p><strong> Code </strong></p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;vector&gt;</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
  <span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">int</span>&gt; arr = {<span class="hljs-number">2</span>,<span class="hljs-number">5</span>,<span class="hljs-number">8</span>,<span class="hljs-number">1</span>,<span class="hljs-number">7</span>,<span class="hljs-number">3</span>};
  <span class="hljs-comment">//made another array to store the answer and then print the output</span>
  <span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">int</span>&gt; ans;
  <span class="hljs-keyword">int</span> n = arr.size();

  <span class="hljs-comment">//starting from n-1 and going till 0</span>
  <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i = n<span class="hljs-number">-1</span>; i &gt;= <span class="hljs-number">0</span>; i--){
    ans.push_back(arr[i]);
  }
  <span class="hljs-built_in">cout</span>&lt;&lt;<span class="hljs-string">"The reversed array is "</span>;
  <span class="hljs-comment">//for printing the array</span>
  <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; ans.size(); i++){
    <span class="hljs-built_in">cout</span>&lt;&lt;ans[i]&lt;&lt;<span class="hljs-string">" "</span>;  
  }
  <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p> The solution to the given problem is correct but let's think out another or more good, optimized approach than this. (The more ways we find to solve the same problem it benefits our brain to do more practice and our problem-solving skills also improve simultaneously).</p>
<p><strong> Approach 2 </strong></p>
<p>In this approach, we will use the concept of <a target="_blank" href="https://leetcode.com/articles/two-pointer-technique/">two pointer technique</a>. (I know I know two pointer technique is a new term for you but don't worry I'll explain this).</p>
<p><em>Two Pointer Technique</em> : This is not some high-level concept, here two pointers i.e. variables or index(start and end indexes) are assigned one pointer is assigned at the start while the other is assigned at the end of the array, these two pointers which are assigned travel towards each other until and unless they meet each other.</p>
<p>Here two indexes i.e. start index and end index would be assigned and then we would swap the start values with the end values, and this would give our reverse array.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1642337001846/FwWWD2qGUq.png" alt="fvvfxv.png" /></p>
<p>This way we will get our reverse array </p>
<p><strong> Code </strong></p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;vector&gt;</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
  <span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">int</span>&gt; arr = {<span class="hljs-number">2</span>,<span class="hljs-number">5</span>,<span class="hljs-number">8</span>,<span class="hljs-number">1</span>,<span class="hljs-number">7</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>};
  <span class="hljs-keyword">int</span> n = arr.size();

  <span class="hljs-comment">//pointer one starting from 0</span>
  <span class="hljs-keyword">int</span> start = <span class="hljs-number">0</span>;

  <span class="hljs-comment">//pointer second starting from the end of the array</span>
  <span class="hljs-keyword">int</span> end = n<span class="hljs-number">-1</span>;
  <span class="hljs-keyword">while</span>(start &lt; end){
      swap(arr[start], arr[end]);
      start++;
      end--;
  }
  <span class="hljs-built_in">cout</span>&lt;&lt;<span class="hljs-string">"The reverse array is "</span>;
  <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; n; i++){
    <span class="hljs-built_in">cout</span>&lt;&lt;arr[i]&lt;&lt;<span class="hljs-string">" "</span>;
  }
  <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>Time Complexity : O(n/2) which is nearly O(n)</p>
<h2 id="heading-question-2">Question 2</h2>
<p>Till here we have solved the first question, now let's come on to the second question Reverse the array after the <strong> m </strong> position. This question can be done using the <strong> approach 2 </strong> of the first question i.e. two pointer technique. Here we have to reverse the array after a particular given position or index. </p>
<p>So, the major change we would do here is assigning start = m + 1, where m is the index after which the array needs to be reversed, the rest of the code would be the same here.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1642339439173/0h9s4LLV8.png" alt="vvdv.png" /></p>
<p><strong> Code </strong></p>
<pre><code class="lang-cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;vector&gt;</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
  <span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">int</span>&gt; arr = {<span class="hljs-number">2</span>,<span class="hljs-number">5</span>,<span class="hljs-number">8</span>,<span class="hljs-number">1</span>,<span class="hljs-number">7</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>};
  <span class="hljs-keyword">int</span> n = arr.size();
  <span class="hljs-keyword">int</span> m = <span class="hljs-number">3</span>;<span class="hljs-comment">//the position after which the array needs to be revered</span>

  <span class="hljs-comment">//pointer one starting after m</span>
  <span class="hljs-keyword">int</span> start = m+<span class="hljs-number">1</span>;

  <span class="hljs-comment">//pointer second starting from the end of the array</span>
  <span class="hljs-keyword">int</span> end = n<span class="hljs-number">-1</span>;
  <span class="hljs-keyword">while</span>(start &lt; end){
      swap(arr[start], arr[end]);
      start++;
      end--;
  }
  <span class="hljs-built_in">cout</span>&lt;&lt;<span class="hljs-string">"The reverse array is "</span>;
  <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; n; i++){
    <span class="hljs-built_in">cout</span>&lt;&lt;arr[i]&lt;&lt;<span class="hljs-string">" "</span>;
  }
  <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>Hurray!, we have completed both the questions by understanding the concepts and then coding the problems. </p>
<p>So, here are some suggestions to solve DSA problems : </p>
<ul>
<li><p>Don't just read and code the question, first take a pen a paper and try to solve the problem by yourself, ask yourself that are you able to build the logic which is needed to solve the problem.</p>
</li>
<li><p>Don't worry if you aren't able to solve at first attempt only, even sometimes I also need more time to solve a problem or can't able to solve the problem at one go. So, it's OK</p>
</li>
<li><p>After spending a sufficient amount of time on that question and, then also you can't able to solve it then take a break for some time and come fresh and then give a try to that question or maybe you can search for the question's solution on YouTube, different coding platforms or seek out to your friends to take some hint of the question and then solve it again by yourself.</p>
</li>
<li><p>Don't hesitate to ask for help from your friends, because it's ok if you don't know that question. It's ok.</p>
</li>
</ul>
<p>Feel free to reach out on <a target="_blank" href="https://twitter.com/_pagariarishabh">Twitter</a> for any problems and questions.</p>
]]></content:encoded></item></channel></rss>