Given an unsorted array arr[] of size N, rotate it by D elements (clockwise).
Input
The first line of the input contains T denoting the number of testcases. First line of each test case contains two space separated elements, N denoting the size of the array and an integer D denoting the number size of the rotation. Subsequent line will be the N space separated array elements
Output
For each testcase, in a new line, output the rotated array
Example
Input
1 2 3 4 5
Output
3 4 5 1 2
#include <iostream>
#include <stack>
using namespace std;
void rotate(int *a,int s,int r) {
stack<int> st;
for(int i=0;i<r;i++) {
st.push(a[i]);
}
for(int j=r;j<s;j++) {
a[j-r] = a[j];
}
for(int k=s-1;k>r+1;k--) {
a[k] = st.top();
st.pop();
}
for(int l=0;l<s;l++) {
cout<<a[l]<<" ";
}
}
int main() {
//code
int T;
cin>>T;
while(T--) {
int N,r;
cin>>N>>r;
int A[N];
for(int i=0;i<N;i++) {
cin>>A[i];
}
rotate(A,N,r);
cout<<endl;
}
return 0;
}
I followed your logic, it seems like there is problem in your backfill part.
for(int k=s-1;k>=s-r;k--) { // change k>r+1 to k>=s-r
a[k] = st.top();
st.pop();
}
sorry my bad, int third for loop in rotate function there should be k>s-r-1
Related
I Am Trying To Find Partition Of Array ,On Condition By Checking Variable x ,when less then x they will be on one side or else on another. but my code need some correction.
HERE am not able to find the error , i will be thankful to you if you help me.
Code is:-
#include<iostream>
using namespace std;
int partition(int arr[],int n,int x){
for(int i=0;i<n;){
if(arr[i]<x){
i++;
}
else if(arr[i]==x){
int temp=arr[i];
arr[i]=arr[n];
arr[n]=temp;
i--;
}
else if(arr[i]>x){
int temp=arr[i];
for(int j=i;j<n;j++){
arr[j]=arr[j+1];
}
arr[n]=temp;
i--;
}
}
return 0;
}
int main(){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int x;
cin>>x;
partition(arr,n,x);
for(int i=0;i<n;i++){
cout<<arr[i]<<"\t";
}
return 0;
}
Input >> array={2,10,15,1,3,15} ,x=10
Expected << {2,1,3,10,15,15}
Output I get << nothing .
The code isn't giving any output because, first, the "cin" and "cout" are in upper case which is syntactically incorrect, secondly, the variable j is in different case in loop statement and body inside the second else-if clause in the partition function, same goes for the "I" in the first for loop in the main() function. Sort this out and you should be good to go.
First in C++ the size of an array must be a compile-time constant. So for example, consider the following examples:
int n = 10;
int arr[n]; //INCORRECT
The correct way to write the above would be:
const int n = 10;
int arr[n]; //CORRECT
Similarly, in your code,
int n;
cin>>n;
int arr[n]; //INCORRECT because n is not a constant expression
Second, in your code, when you wrote:
arr[n] = temp; Undefined behavior
you're going out of bounds and so you have undefined behavior.
Solution
You can use std::stable_partition and std::vector to solve your problem as shown below:
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
int n;
std::cout <<"Enter n:"<<std::endl;
std::cin >> n;
std::vector<int> arr(n); //create a vector of size n instead of an array
std::cout<<"Enter elements: "<<std::endl;
//iterate and take input from user
for(int &elem: arr){
std::cin >> elem ;
}
int x;
std::cout << "Enter x: "<<std::endl;
std::cin>>x;
//use std::partition
std::stable_partition(arr.begin(), arr.end(), [x](int i){return (i < x);});
std::cout<<"This is the partitioned vector: "<<std::endl;
for(int i=0;i<n;i++)
{
std::cout<<arr[i]<<"\t";
}
return 0;
}
Output
The output of the above program is as follows:
Enter n:
6
Enter elements:
2
10
15
1
3
15
Enter x:
10
This is the partitioned vector:
2 1 3 10 15 15
which can be seen here.
I am trying to solve the program of array rotation. I am getting segmentation error in the code. Can someone please tell where is the problem in this code?
this is the question
Given an unsorted array arr[] of size N, rotate it by D elements (clockwise).
The first line of the input contains T denoting the number of testcases. First line of each test case contains two space separated elements, N denoting the size of the array and an integer D denoting the number size of the rotation. Subsequent line will be the N space separated array elements.
and i have solved it with the following code.
#include <iostream>
using namespace std;
int* rotate(int ar[],int n, int m)
{static int temp[100];
for(int i =0;i<m;i++)
{
temp[i]=ar[i];
}
for(int j =m;j<n;j++)
{
ar[j-m]=ar[j];
}
int x=0;
for(int k =n-m;k<n;k++)
{
ar[k]=temp[x];
x++;
}
return ar;
}
int main() {
//code
int t, n , m;
cin>>t;
while(t>0)
{
cin>>n>>m;
int arr[n];
int * ptr;
for(int i = 0 ;i<n;i++)
{
cin>>arr[i];
}
ptr=rotate(arr,n,m);
for(int j=0;j<n;j++)
cout<<ptr[j]<<" ";
cout<<endl;
t--;
}
return 0;
}
If m > n then it crashes in the first for() loop in rotate as you index past the end of arr.
If m < 0 it crashes in the 2nd loop as it index before arr.
There are probably more cases.
I have a permutation of a sequence of natural numbers incrementing from 1 as an array. How can I determine whether the array can be sorted using rotation of 3 consecutive elements?
I have implemented an algorithm in which basically I'm comparing the indices of the array with the element at that index in the array. If they are not equal then I call the function choose_indices() which first finds the element to be swap at the right position in the array and after finding it, selects the 3 consecutive elements including the number to be swapped and rotates them. After performing n-1 rotations for an array of size n, the array is sorted. This implementation returns true if an array can be sorted using 3 consecutive element rotation but timeouts for an array if the array can't be sorted using this method.
using namespace std;
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<cstring>
int n;
void rotate(vector<int> &arr,int end,int mid,int start)
{
int temp=arr[start];
arr[start]=arr[end];
arr[end]=arr[mid];
arr[mid]=temp;
}
void choose_indices(vector<int> &arr,int s,int q)
{
for(int l=q;l<n;l++)
{
if(arr[l]==s)
{
if(l-q>=2)
{
rotate(arr,l,l-1,l-2);
break;
}
else
{
rotate(arr,l+1,l,l-1);
break;
}
}
}
}
int main()
{
vector<int> arr;
int q,count=0;
cin>>q;
for(int i=0;i<q;i++)
{
cin>>n;
count=0;
for(int i=0,p;i<n;i++)
{
cin>>p;
arr.push_back(p);
}
for(int j=0,k=1;j<n && k<n; )
{
if(arr[j]!=k)
{
choose_indices(arr,k,j);
if(arr[j]==k)
{
j++;
k++;
count++;
}
}
else
{
j++;
k++;
count++;
}
}
if(count==n-1)
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
arr.clear();
}
}
Sample Input:
1 2 3 5 4
For this input, my code gives runtime error.
How can I find if the array given cannot be sorted using the rotation of 3 consecutive elements?
Rotating 3 adjacent elements will always cancel 2 inversions if present or it will introduce 2 inversions.
Consider this:
1 2 3 5 4
Has only 1 inversion, no matter how many times you rotate , you can never cancel that inversion without introducing other inversions as you will be always rotating 3 consecutive elements.
So just count the number of inversions and if its odd then the answer is NO, otherwise YES. There are efficient algorithms out there to count the number of inversions(like merge sort).
using namespace std;
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<cstring>
int n;
void rotate(vector<int> &arr,int end,int mid,int start)
{
int temp=arr[start];
arr[start]=arr[end];
arr[end]=arr[mid];
arr[mid]=temp;
}
void choose_indices(vector<int> &arr,int s,int q)
{
for(int l=q;l<n;l++)
{
if(arr[l]==s)
{
if(l-q>=2)
{
rotate(arr,l,l-1,l-2);
break;
}
else
{
rotate(arr,l+1,l,l-1);
break;
}
}
}
}
int main()
{
vector<int> arr;
int q,count=0;
cin>>q;
for(int i=0;i<q;i++)
{
cin>>n;
count=0;
for(int i=0,p;i<n;i++)
{
cin>>p;
arr.push_back(p);
}
//Counting the number of inversion in the array
int ctiv=0;
for(int r=0;r<n;r++)
{
for(int s=r+1;s<n;s++)
{
if(arr[r]>arr[s] && r<s)
{
ctiv++;
}
}
}
if(ctiv%2!=0)
{
cout<<"NO"<<endl;
}
else
{
for(int j=0,k=1;j<n && k<n; )
{
if(arr[j]!=k)
{
choose_indices(arr,k,j);
if(arr[j]==k)
{
j++;
k++;
count++;
}
}
else
{
j++;
k++;
count++;
}
}
if(count==n-1)
{
cout<<"YES"<<endl;
}
arr.clear();
}
}
}
This is the algorithm i designed which finds if the given algorithm can be sorted and if it can be sorted, it sorts the given array by performing the necessary 3 consecutive rotations.
The goal of my program is to find the number entered by user in an array of integers (array was created automatically), and to show the index of this number (or numbers, if they occurs several times). It works correctly when the desired number occurs only once in array. For example, if there is an array
7 8 0 4 2 7 2
and user entered "8", the output of program will be
Index of the number you entered is: 2
But when we have array:
0 5 3 9 3 7 2
And the user entered "3", the output will be
Index of the number you entered is: 3
And I wonder how to make the program include second "3" number which has index 5. The code of program:
#include <iostream>
#include <ctime>
#include <stdlib.h>
using namespace std;
int i, N;
int LinearSearch(int Array[], int searchValue)
{
for (i=0; i<N; i++)
{
if (Array[i]==searchValue)
return i;
}
return -1;
}
int main()
{
int searchValue, Array[1000];
cout<<"Size of array: ";
cin>>N;
cout<<"Array: ";
for (i=0; i<N; i++)
{
Array[i]=rand()%10;
cout<<Array[i]<<" ";
}
cout<<"Search value: ";
cin>>searchValue;
if (LinearSearch(Array, searchValue)==1)
cout<<"\nIndex of the number you entered is: "<<LinearSearch(Array, searchValue)+1;
else
cout<<"\nNothing found";
}
You can do it in two ways:
1. Change the LinearSearch's return value to vector, write it like this:
vector<int> LinearSearch(int Array[], int searchValue)
2.Add a vector reference variable in the parameters, it should like this:
int LinearSearch(int Array[], int searchValue, vector<int> &results)
And the the method body in LinearSearch should have little change accordingly.
Because you return from the search function as soon as the value is located:
for (i=0; i<N; i++)
{
if (Array[i]==searchValue)
return i; // <-- as soon as we get here, we break the loop
}
therefore, you will get the first position in which searchValue is located, which is 2 (0-based). Thus, you get 2+1 = 3. To get the last one, you will have to remove the early exit, and keep the current index in a variable, like this:
int LinearSearch(int Array[], int searchValue) {
int index = -1;
for (i = 0; i < N; i++) {
if (Array[i]==searchValue) {
index = i;
}
}
return index;
}
So I have to print Floyd's triangle but like this:
7
1
2 3
4 5 6
7 * * *
Here is my code, I just can't figure out how to print the * at the end of the last line if there is any space left.
#include<iostream>
#include<cmath>
using namespace std;
int main ()
{
int n;
cin>>n;
int br=1;
for (int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
if(br<=n)
cout<<br<<" ";
br++;
}
if(br<=n)
cout<<endl;
}
}
Here is the modified code:
#include<iostream>
#include<cmath>
using namespace std;
int main ()
{
int n, i, j;
cin>>n;
int br=1;
for (i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
if(br>n)
break;
cout<<br<<" ";
br++;
}
if(br>n)
break;
cout<<endl;
}
for(int k = j; k <= i; k++)
{
cout<<"* ";
}
}
Note that on nth line, there are nth numbers. So, for every line, you must count the numbers you wrote already. So, when you write the number that you need you have written k numbers. Now, you need to add n-k stars.