You are given an array A of size N. You need to print elements of A in an alternate order (starting from index 0).
Example:
Input:
N = 6
A[] = {10, 20, 30, 40, 50, 60}
Output:
10 30 50
#include< bits/stdc++.h>
using namespace std;
void print(int ar[], int n)
{
for (int i=0; i< n; i++)
{
if(i%2==0)
{
cout<< ar[i]<<" ";
}
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
int ar[100001]={0};
int n;
cin>>n;
for(int i=0;i< n;i++)
cin>>ar[i];
print(ar,n);
cout<< endl;
}
return 0;
}
You are given two integers A and B. Your task is to print the remainder when A is divided by B.
INPUT:
The first line contains an integer T, the total number of test cases.
Then T lines follow, each line contains two Integers A and B.
OUTPUT:
For each test case, find the remainder when A is divided by B, and display it in a new line.
Test Case: 1
INPUT:
2
4 7
13 6
OUTPUT:
4
1
You are given an integer N. Your task is to print the number in reverse order.
INPUT:
The first line contains an integer T, the total number of test cases.
Then follow T lines, each line contains an integer N.
OUTPUT:
For each test case, display the reverse of the given number N, in a new line.
Test Case: 1
INPUT:
2
3456
8796
OUTPUT:
6543
6978
#include
int main(void)
{
int T;
scanf("%d",&T);
while(T--)
{
int A,rem,rev=0;
scanf("%d",&A);
while(A!=0)
{
rem = A%10;
rev = rev*10+rem;
A=A/10;
}
printf("%d\n",rev);
}
return 0;
}
You are given with an array A of size N, print the reverse of it.
Input:
First line of input contains an integer denoting the test cases 'T'.
Each testcase contains two lines of input.
First line contains N the size of the array A.
The second line contains the elements of the array.
Output:
For each testcase, in a new line, print the array in reverse order.
#include
#include
using namespace std;
int main()
{
int T;
cin >>T;
int arr[100];
while (T--)
{
int n;
cin >>n;
for (int i=0; i< n; i++)
{
cin >> arr[i];
}
for (int i=n-1; i>=0; i--)
{
cout << arr[i] << " ";
}
cout<< endl;
}
return 0;
}
You are 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.
The next line will be the N space-separated elements of the array.
Output:
For each testcase, in a new line, output the final rotated array.
Test Case: 1
INPUT:
1
9 3
2 3 4 5 6 7 8 9 1
OUTPUT:
5 6 7 8 9 1 2 3 4
#include
using namespace std;
int main()
{
int T;
cin >> T;
while(T--)
{
int N, D;
cin >> N >> D;
int arr[N];
for(int i=0; i< N; i++)
{
cin >> arr[i];
}
for(int i=D; i< N; i++)
{
cout << arr[i] <<" ";
}
for(int i=0; i< D; i++)
{
cout << arr[i] <<" ";
}
cout << endl;
}
return 0;
}