
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to get blocks position after right side rotation
Suppose we have an array A with n elements. A[i] represents there are A[i] number of blocks stacked on top of each other at ith column. The entire blocks are inside a closed transparent boundary box. Now if we rotate the entire big box clockwise 90°, then due to change of gravity direction, the blocks will fall, after than reverse it to its previous orientation. Then find the new array like A after these operations.
Problem Category
This problem falls under sorting problems. Sorting is a very common problem while we are talking about different problem solving algorithms in computer science. As the name suggests, sorting indicates arranging a set of data into some fashion. We can arrange them in nondecreasing order or non-increasing order in general. Otherwise sorting can be also takes place in a pre-defined manner. For the string based problems, sometimes we refer lexicographical sorting to arrange letters in dictionary fashion. There are plenty of different sorting techniques with certain variations and their time and space complexity. To date, the lower-bound of the time complexity for comparison based sorting techniques is O(n*log n). However there are some mechanical sorting techniques like bucket sort, radix sort, counting sorts are there whose time complexity is linear O(n) in time. For further reading, please follow the link below −
https://www.tutorialspoint.com/data_structures_algorithms/sorting_algorithms.htm
So, if the input of our problem is like A = [3, 2, 1, 2], then the output will be [1, 2, 2, 3]
Steps
To solve this, we will follow these steps −
sort the array A for initialize i := 0, when i < size of A, update (increase i by 1), do: print A[i]
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; void solve(vector<int> A){ sort(A.begin(), A.end()); for (int i = 0; i < A.size(); i++) cout << A[i] << ", "; } int main(){ vector<int> A = { 3, 2, 1, 2 }; solve(A); }
Input
{ 3, 2, 1, 2 }
Output
1, 2, 2, 3,
- Related Articles
- C++ Program to get number at position k after positioning n natural numbers
- C++ Program to Perform Right Rotation on a Binary Search Tree
- C++ program to check if two images match after rotation and translation
- Program to find length of longest palindromic substring after single rotation in Python
- Golang Program to trim a string from the right side
- Position after taking N steps to the right and left in an alternate manner in C++
- Swift Program to get array elements after N elements
- Golang Program to get array elements after N elements
- What blocks Ruby, Python to get Javascript V8 speed?
- How to detect point on canvas after canvas rotation in HTML5
- JavaScript Program To Delete Nodes Which Have A Greater Value On Right Side
- Program to find next board position after sliding the given direction once in Python
- Program to find out the position of a ball after n reversals in Python
- How to position text to top right position on an image with CSS
- Golang Program to pad a string with 0's on the right side
