

Increment odd positioned elements by 1 and decrement even positioned elements by...
source link: https://www.geeksforgeeks.org/increment-odd-positioned-elements-by-1-and-decrement-even-positioned-elements-by-1-in-an-array/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Increment odd positioned elements by 1 and decrement even positioned elements by 1 in an Array
- Difficulty Level : Easy
- Last Updated : 26 Apr, 2021
Given an array arr[], the task is increment all the odd positioned elements by 1 and decrement all the even positioned elements by 1.
Examples:
Input: arr[] = {3, 6, 8}
Output: 4 5 9
Input: arr[] = {9, 7, 3}
Output: 10 6 4
Approach: Traverse the array element by element and if the current element’s position is odd then increment it by 1 else decrement it by 1. Prin the contents of the updated array in the end.
Below is the implementation of the above approach:
- Python3
- Javascript
// C++ implementation of the approach
#include <bits/stdc++.h>
using
namespace
std;
// Utility function to print
// the contents of an array
void
printArr(
int
arr[],
int
n)
{
for
(
int
i = 0; i < n; i++)
cout << arr[i] <<
" "
;
}
// Function to increment all the odd
// positioned elements by 1 and decrement
// all the even positioned elements by 1
void
updateArr(
int
arr[],
int
n)
{
for
(
int
i = 0; i < n; i++)
// If current element is odd positioned
if
((i + 1) % 2 == 1)
arr[i]++;
// If even positioned
else
arr[i]--;
// Print the updated array
printArr(arr, n);
}
// Driver code
int
main()
{
int
arr[] = { 3, 6, 8 };
int
n =
sizeof
(arr) /
sizeof
(arr[0]);
updateArr(arr, n);
return
0;
}
4 5 9
Time Complexity: O(n)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
Recommend
-
20
PHP RFC: Increment/Decrement Fixes
-
8
Discussion (30) 2010-01-22 19:16:58 by Baughn: It all makes sense now! o_O 2010-01-22 19:42:14 by Ampersan...
-
15
Conversation Member
-
5
VS Code Gold (14 Part Series) When you are building websites, sometimes you need to experiment with different values to get things just right. You are playi...
-
4
Rails is a large framework with a lot of handy tools built-in for specific situations. In this series, we'll take a look at some of the lesser-known tools hidden in Rails' large codebase. In this article, we'll explain the increment
-
10
C++ Program to Check Whether Number is Even or OddSkip to content
-
4
Increment and Decrement Column Value in Laravel 807 views 9 months ago Laravel In this article we will share with y...
-
9
Why are many Windows user interface elements positioned at multiples of 4 or 8 pixels?
-
10
C Program to Segregate Even and Odd numbersSkip to content
-
10
C# Program to Overload Unary Increment (++) and Decrement (–) Operators ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK