I've been stuck on this problem for a pretty Long while. I always get "Time limit exceeded" when I submit the code.
My solution is to input the items of the array then determine the largest number in the array and diplay it along with the elements following it and so on.
How can I make my algorithm more efficient?
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int T, n;
cin >> T;
while (T--) {
//inputting p[n]
scanf_s("%d", &n);
int* p = new int[n];
for (int i = 0; i < n; i++) {
scanf_s("%d", &p[i]);
}
while (n != 0) {
//*itr = largest element in the array
auto itr = find(p, p + n, *max_element(p, p + n));
int index = distance(p, itr);
for (int i = index; i < n; i++) {
printf("%d\n", p[i]);
}
//deleting element by decreasing n:
n = index;
}
delete[] p;
}
return 0;
}
You solution is O(n^2), too slow.
A O(n) solution is obtained by iteratively calculating the position of the max element until a given index i.
#include <iostream>
#include <vector>
#include <algorithm>
//using namespace std;
int main() {
int T;
std::cin >> T;
while (T--) {
//inputting p[n]
int n;
std::cin >> n;
std::vector<int> p(n);
for (int i = 0; i < n; i++) {
std::cin >> p[i];
}
std::vector<int> max_before(n);
max_before[0] = 0;
for (int i = 1; i < n; ++i) {
if (p[i] > p[max_before[i-1]]) {
max_before[i] = i;
} else {
max_before[i] = max_before[i-1];
}
}
while (n != 0) {
int index = max_before[n-1];
for (int i = index; i < n; i++) {
std::cout << p[i] << " ";
}
//deleting element by decreasing n:
n = index;
}
std::cout << '\n';
}
return 0;
}
Related
You have been given an array/list(ARR) of size N. You need to swap every pair of alternate elements in the array/list.
You don't need to print or return anything, just change in the input array itself.
#include <iostream>;
using namespace std;
void printArr(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i]<<i;
}
void UpdateArr(int arr[], int n)
{
int i = 0, j = n - 1;
while (i < j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i += 2;
j -= 2;
}
cout<<' printArr(arr[], n)';
}
int main()
{
int t;
cin>> t;
int n;
cin>> n;
int input[100];
for(int i=0; i<n; i++) {
cin >>input[i];
}
int arr[100] ;
n = sizeof(arr) / sizeof(arr[0]);
UpdateArr(arr, n);
return 0;
}
I'm not sure what are you exactly expecting the output to be (pls edit it and show the expected output) but I think this is what you need to do
#include <iostream>
#include <iomanip>
using namespace std;
void UpdateArray(int Arr[], size_t n) {
for (size_t i = 0; i < n / 2; i++) {
int Holder = Arr[i];
Arr[i] = Arr[~i + n];
Arr[~i + n] = Holder; } }
int main() {
int Arr[7] = { 1,2,3,4,5,6,7 };
UpdateArray(Arr, 7);
for (int i = 0; i < 7; i++) {
std::cout << Arr[i] << "\n"; }
return 0; }
size_t is like an int but it can't go into negative, but it can take bigger positive numbers, you can replace it with int, it shouldn't make a difference.
so we loop through half the array, replacing first items with last, the [~i + n] flips the value to the other side, so like index 4 in a array size of 20 will become 15
As you see in my code, I have to return an array through return function. What necessary thing do I have to change in my code to disable the warning of return type? When I run this code, it gives me a warning. I have to remove this warning:
warning: no return statement in function returning non-void [-Wreturn-type]
#include <bits/stdc++.h>
using namespace std;
int sort(int a[], int n)
{
int zeros = 0;
for (int i = 0; i < n; i++)
{
if (a[i] == 0)
{
zeros++;
}
}
int k = 0;
while (zeros--)
{
a[k++] = 0;
}
while (k < n)
{
a[k++] = 1;
}
}
int main()
{
int n;
cout << "Enter the n value : ";
cin >> n;
int a[n];
cout << "Enter the array elements : ";
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
sort(a, n);
for (int i = 0; i < n; i++)
{
cout << a[i];
}
return 0;
}
int sort(int a[], int n)
Your sort function is declared to return an int, but you didn't return anything. If you don't want to return anything, declare it using void.
Also, VLA is not valid C++. You should use std::vector instead.
So, your code should look like this.
#include <iostream>
#include <vector>
void sort(std::vector<int> &a) // no need for n
{
int zeros = 0;
for (int i = 0; i < a.size(); i++) // use member function size() instead for n
{
if (a[i] == 0)
{
zeros++;
}
}
int k = 0;
while (zeros--)
{
a[k++] = 0;
}
while (k < n)
{
a[k++] = 1;
}
}
int main()
{
int n;
std::cout << "Enter the n value : ";
std::cin >> n;
std::vector<int> a(n);
std::cout << "Enter the array elements : ";
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
sort(a);
for (cons auto &i : a) // range-base for loop, recommend
{
std::cout << a[i];
}
return 0;
}
Note: using namespace std; and #include <bits/stdc++.h> are both bad practice, so avoid using it.
In an array of N elements (N is given), find the smallest element from the first zero element to the end of the array. If there are no zero elements in the array, display a message about it.
Can someone fix this for me please?
#include<iostream>
using namespace std;
int main() {
int i, n;
cout << "N= "; cin >> n;
if (n > 0) {
int *a = new int[n];
for (i = 0; i < n; i++) {
cin >> a[i];
}
for (i = 0; i < n; i++) {
if (a[0] > a[i])
a[0] = a[i];
}
cout << "\nMin:" << a[0];
delete[] a;
}
return 0;
}
I couldn't find the problem with your code so I am showing how I approached finding a minimum element from a given array:
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int minNo=INT16_MAX; // to assign max. possible value to minNo
for(int i=0;i<n;i++){
minNo=min(arr[i],minNo);
}
cout<<minNo<<endl;
return 0;
}
You can simply check it in the same loop in which you looking for smallest element.
#include <iostream>
#include <climits>
using namespace std;
int main() {
int i, n;
bool isZeroElement = false;
cout << "N= "; cin >> n;
if (n > 0) {
int *a = new int[n];
for (i = 0; i < n; i++) {
cin >> a[i];
}
int minElement = INT_MAX;
for (i = 0; i < n; i++) {
if (!isZeroElement && a[i] == 0)
{
isZeroElement = true;
continue;
}
if (isZeroElement && minElement > a[i])
minElement = a[i];
}
if (!isZeroElement)
cout << "There is no zero element in the array\n";
else
cout << "\nMin:" << minElement;
delete[] a;
}
return 0;
}
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
typedef long long int ll;
ll swapCount(ll arr[], ll n)
{
ll count = 0;
ll lastNonZeroElementIndex = 0;
for (ll i = 0; i < n; ++i) {
if (arr[i]) {
lastNonZeroElementIndex = i + 1;
++count;
}
}
return lastNonZeroElementIndex - count;
}
int main()
{
ll t, n;
cin >> t;
while (t--) {
/* code */
cin >> n;
ll arr[n];
for (ll i = 0; i < n; ++i) {
cin >> arr[i];
}
cout << swapCount(arr, n) << endl;
}
return 0;
}
Order should be in-place.
What I am doing is, I am storing count of non-zero element and index of last non-zero element. And returning the difference of these two.
can you tell me where I am making mistake?
Problem Statement
I'm writing a program that has a user input integers into an array, calls a function that removes duplicates from that array, and then prints out the modified array. When I run it, it lets me input values into the array, but then gives me a "Segmentation fault" error message when I'm done inputing values. What am I doing wrong?
Here is my code:
#include <iostream>
using namespace std;
void rmDup(int array[], int& size)
{
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
if (array[i] == array[j])
{
array[i - 1 ] = array[i];
size--;
}
}
}
}
int main()
{
const int CAPACITY = 100;
int values[CAPACITY], currentSize = 0, input;
cout << "Please enter a series of up to 100 integers. Press 'q' to quit. ";
while (cin >> input)
{
if (currentSize < CAPACITY)
{
values[currentSize] = input;
currentSize++;
}
}
rmDup(values, currentSize);
for (int k = 0; k < currentSize; k++)
{
cout << values[k];
}
return 0;
}
Thank you.
for (int i = 0; i < size; i++)
{
for (int j = i + 1; j < size; j++)
{
if (array[i] == array[j])
{
array[i - 1 ] = array[i]; /* WRONG! array[-1] = something */
size--;
}
}
}
If array[0] and array[1] are equal, array[0-1] = array[0], meaning that array[-1] = array[0]. You are not supposed to access array[-1].
I wouldn't make it even possible to create duplicates:
int main()
{
const int CAPACITY = 100;
cout << "Please enter a series of up to 100 integers. Press 'q' to quit. ";
std::set<int> myInts;
int input;
while (std::cin >> input && input != 'q' && myInts.size() <= CAPACITY) //note: 113 stops the loop, too!
myInts.insert(input);
std::cout << "Count: " << myInts.size();
}
And do yourself a favour and don't use raw arrays. Check out the STL.
#include <algorithm>
#include <iostream>
#include <iterator>
using namespace std;
int main()
{
vector<int> vec = {1,1,2,3,3,4,4,5,6,6};
auto it = vec.begin();
while(it != vec.end())
{
it = adjacent_find(vec.begin(),vec.end());
if(it != vec.end())
vec.erase(it);
continue;
}
for_each(vec.begin(),vec.end(),[](const int elem){cout << elem;});
return 0;
}
This code compiles with C++11.
#include<iostream>
#include<stdio.h>
using namespace std;
int arr[10];
int n;
void RemoveDuplicates(int arr[]);
void Print(int arr[]);
int main()
{
cout<<"enter size of an array"<<endl;
cin>>n;
cout<<"enter array elements:-"<<endl;
for(int i=0;i<n ;i++)
{
cin>>arr[i];
}
RemoveDuplicates(arr);
Print(arr);
}
void RemoveDuplicates(int arr[])
{
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;)
{
if(arr[i]==arr[j])
{
for(int k=j;k<n;k++)
{
arr[k]=arr[k+1];
}
n--;
}
else
j++;
}
}
}
void Print(int arr[])
{
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
}